Is It Possible to Get a Macro To Trigger by Selecting Text in a Particular App?

I am trying to create a macro to save an opened PDF file in Safari to my Downloads folder. I am trying to trigger the macro by selecting some text in the document which will be used as the filename.
Is it possible to trigger a macro by selecting text? I know some other apps like Popclip can do this but I will prefer if the whole sequence of actions can be executed by KBM to reduce any redundancy. I also think it will be nice to have this as a trigger in KBM.

There is no trigger for just selecting text. You will need to trigger the macro some other way (via Popclip, or with some other trigger like a hot key or whatever).

Hey Mirizzi,

To save a PDF that’s already open in Safari by downloading it again type:

Cmd-L
Option-Return

To just export it from cache save it with Cmd-S.

This gives you the opportunity to name it as you wish.

The Keyboard Maestro sequence would go something like:

Copy
Save
Pause Until button ā€˜Save’ is enabled.
Paste
Type Cmd-Shift-G
Paste or type /Users/chris/Downloads
Press button 'Go’
Press button ā€˜Save’

Since I have Default Folder I’d actually script getting the selected text from Safari, the name change, and the save-location change. This bypasses the clipboard and is faster. Keyboard Maestro would still be used to activate the save, run the script, and press the final ā€˜Save’ button.

-Chris

I’m thinking you could map a KM workflow to a BetterTouchTool action that actually copies and pastes the selected text and saves the file

1 Like

Thanks. I tried your macro for a PDF that is embedded in the page, http://joliclic.free.fr/html/object-tag/en/object-application.html#pdf. I could not get it to save the PDF.
How can I save embedded PDFs and non-embedded PDFs using a single macro?

My initial approach was to select the title text in the PDF, select a right-mouse click and then select ā€œOpen in Previewā€. After opening in Preview, the PDF can then be saved to a particular folder using the copied title as its new filename.

Is it possible to copy the PDF file in the background from the cache folder and rename it before pasting in the Documents folder?

Hey Mirizzi,

Were you speaking to me? Unless your reply is directly after someone else’s post it’s a good idea to address them, so they know who you’re speaking to.

The answer is that you have to be able to test for content.

Such tests might go like:

tell application "Safari"
  set _url to URL of front document
  if _url ends with ".pdf" and (source of front document) = "" then
    return true
  end if
end tell

If this returns true then the loaded document in Safari is almost certainly a PDF file.

If it’s false then you can run something like this following script to pick up linked pdf files.

There will probably be cases where this technique won’t work, but you can inspect the code of the web page to see if it’s possible to pull out the links you want.

------------------------------------------------------------

set linkList to safari_links(".*\\.pdf", "*", "href")

if linkList ≠ {} then
	tell current application
		set myPicks to choose from list linkList default items (item 1 of linkList) with multiple selections allowed and empty selection allowed
	end tell
end if

------------------------------------------------------------
--Ā» HANDLERS
------------------------------------------------------------
on safari_links(regexStr, tagName, tagType)
	set js to "function in_array (array, item) {
	for (var i=0; i < array.length; i++) {
		if ( array[i] == item ) {
			return true;}}
	return false;}
	var a_tags = document.getElementsByTagName('" & tagName & "');
	var href_array = new Array();
	var reg = new RegExp(/" & regexStr & "/i);
	for (var i=0; i < a_tags.length; i++) {
		var href = a_tags[i]." & tagType & ";
		if ( reg.test(href)) {
			if ( !in_array(href_array, href)) {
				href_array.push(href);}}}
 href_array;"
	
	try
		tell application "Safari" to set linkList to do JavaScript js in document 1
		if linkList = missing value then set linkList to {}
	on error
		set linkList to {}
	end try
	
	return linkList
	
end safari_links
------------------------------------------------------------

So. Your task goes from fairly simple to fairly complicated, and there’s no one-size-fits-all answer.

When I undertake something like this I usually write a script that works for my specific use-case.

Later I find a situation that breaks my script, and I add code to account for the new use-case if possible.

Rinse and repeat.

-Chris

There is now (seven years later) the option to have the "This Device Key" as a trigger. While this isn't a selection per se executing the macro, the click and release parts of making a selection can now be used.

If configured to have the RELEASE of the device key along with a modifier be the trigger, then that trigger could execute the macro that does the rest of the work.

To test on an MBP, I have releasing the trackpad with the Control key down execute a copy action, and clicking the trackpad with the Command key down executes a paste command.

1 Like

Thanks for the tip. I was not aware of that. With a double click on a word it can be copied directly to the clipboard (trigger: "ist tapped twice") Just to name one of the possibilities. Nice. I'll have to think about how else to use that.