PopClip extension

I couldn’t find a PopClip extension for Tinderbox, so during a particularly dull meeting, I came up with this little bit of AppleScript.

It looks for .tbx file in your Documents folder called, ahem, “WorkingMemory.tbx”, and if it finds it opens it, and if it doesn’t creates it… and then pastes the selection that was current when you invoked PopClip into a timestamped note, ready for processing…

If you have PopClip installed, save this as “SaveToTBX.popcliptxt”, open it, and PopClip should do the rest. You’ll have to run the guantlet of System Events security the first time you run it, but I’ve been using it all afternoon and it’s been working a treat.

-- #popclip
-- name: SaveToTBX
-- icon: symbol:shippingbox
-- language: applescript

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set notesPath to (path to documents folder as text) & "WorkingMemory.tbx"

tell application "System Events"
  set fileExists to exists file notesPath
end tell

tell application "Tinderbox 10"
  activate
  if fileExists then
    -- If the file exists
    open file notesPath
    activate
  else
    -- Then open the newly created file in TinderBox 10
    set myNewDocument to make new document with properties {name:"Momento Mori"}
    save myNewDocument in POSIX path of notesPath
    
  end if
  
  tell front document
    set timestamp to do shell script "date '+%Y-%m-%d %H:%M:%S'"
    set myNewNote to make new note with properties {name:"My New Note " & timestamp}
    tell myNewNote
      set text of myNewNote to "{popclip text}"
      set value of (attribute named "Badge") to "document2"
    end tell
    save
  end tell
  
end tell

13 Likes

Richard, that is a great one, thank you! :grinning: It works really fine and so fast! It would be a help if it could add a note about the source of the clipping as an attribute, e.g. the app name, or if from an email the from: address, or if from a web page the URL. Do you know how to do this?

How dreadfully rude of me! Yes, quite right, here’s an updated script (GitHub Repo), that at least works for web pages. I’ve add the option to change the target file by specifing it in the line near the top (i.e. set targetTinderboxFile to "WorkingMemory").

Alas, it only uses the variables available natively in PopClip, because trying to figure out the name of the front document in the source app, usually ends in failure… switching between too many apps at that the point the script is running, perhaps?

-- #popclip
-- name: SaveToTBX2
-- icon: symbol:shippingbox
-- language: applescript

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set targetTinderboxFile to "WorkingMemory3"
set notesPath to (path to documents folder as text) & targetTinderboxFile & ".tbx"

tell application "System Events"
	set fileExists to exists file notesPath
end tell

set appName to "{popclip app Name}"
set sourceUrl to "{popclip browser url}"
set docName to "{popclip browser title}"

if docName is "" then
	set docName to "Document"
end if

tell application "Tinderbox 10"
	activate
	if fileExists then
		-- If the file exists
		open file notesPath
		activate
	else
		-- Then open the newly created file in TinderBox 10
		set myNewDocument to make new document with properties {name: targetTinderboxFile}
		save myNewDocument in POSIX path of notesPath
		
	end if
	
	tell front document
		set timestamp to do shell script "date '+%Y-%m-%d %H:%M:%S'"
		set myNewNote to make new note with properties {name:docName & " - " & timestamp}
		tell myNewNote
			set text of myNewNote to "{popclip text}"
			set value of (attribute named "Badge") to "document2"
			set value of (attribute named "SourceURL") to sourceUrl
			set value of (attribute named "DisplayedAttributes") to "SourceURL;" & value of (attribute named "DisplayedAttributes")
		end tell
		save
	end tell
	
end tell
2 Likes

So fast! Thank you! Works great for URLs! This is so useful!

Thank you for this extension - very useful!!

Your fine PopClip extension copies plain text only, so when it copies links on web pages it copies just the text part of the link, not the underlying URL. Since there is a source link, that is no problem at all. I am curious: Would it be possible to copy a working link? Or is PopClip just able to capture plain text?

I would reeeally love to learn how to code … Must be great to build the software I want on my own …

Picky, picky :wink:

Ok, take … err 3 ? Herewith, an additional extension for capturing HTML as Markdown. It is possible to capture raw HTML, but it’s pretty unreadable. Converting it to markdown means that you get your URLs in a very clean way and still retain access to the original text.

See the updated GitHub Repo.

2 Likes

Yes! Works! Great! Again! Thank you so much! I usually love Take 6, but this time take 3 is super, too. :smile:

Hi Richard
The script works for new document, however I am getting an error with an existing document. I ran the script through Script Debugger and here is the error

When run on my system, it is saying it cannot create a document because the document does not exist.

Pathname + FileName has been edited in your script and are correct in my code. Nontheless, same error appears.

Tom

Hi Tom,

I’ve just re-downloaded the extensions from the repo, and run them. The seem to work fine. So, perhaps a little bit more diagnostic info might help?

Assumption: you have an existing file that you want to use. Is it in the documents folder? If not, what path is it on?

What is the name of the file that you’ve set on line 11, if not “WorkingMemory” ? Does it have any special characters?
If you’re using Script Debugger, what would be more useful is to see the state of the variables at the point just before it fails.

Hi Richard

I redownloaded SaveToTbx popclip extension for this test.
The only edit I made was to the path statement: here
image

  1. Yes the file is in a folder in the documents folder
  2. I tested it with an existing file and with the WorkingMemory.
    WorkingMemory test: it works once perfectly, copies the text and creates the file. THEN, keeping the file open, I try it again and I get the error message.
    If I try to use your SaveToTbx with an existing file…I get the same error.
    Its almost like, if the file is created, it does not seem to work. I am on Sequoia 15.3.2.
  3. No special characters…I am using BBEdit for verification. all text.
  4. Script Debugger state…Newbie to Applescript and Script Debugger, not sure how to find the state. Sorry

Enhancement request: whenever you have time and if you like the idea

One enhancement I would love to see after you fix this bug, (if it is not too much trouble) , is to ask for user input for the title of note then copy the selected text as the text in tinderbox.

I am not sure if popclip allows this but… I only ask because the scripting language seems to be Applescript.

Here is the screenshot of the error from Popclip

Tom