Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I add a button to the Safari toolbar that, when clicked, executes a given AppleScript?

share|improve this question
up vote 2 down vote accepted

I don't think it can be done unfortunately. To put a button on the toolbar, you need to write a Safari Extension, which are restricted in what they can do. They can't execute any sort of arbitrary code outside of Safari. You can read more on developer.apple.com.

share|improve this answer

Another solution, and this is really dependant on what you want to do, is to use Services to execute your Applescript.

It doesn't create a toolbar icon, but will allow you to create a service from your AppleScript. You can then define a shortcut key in the System Preferences Keyboard Services pane

This page over at automatedworkflows.com shows an example as well as a link to the rather useful ThisService program.

share|improve this answer

A workaround Safari Extension, could be to use a bookmark in Safari "Favorites Bar".

Use the folder action handler on opening folder this_folder

-- Simple script handler. When clicking the bookmark Finder opens a window to the enclosing folder of the script. However the handler closes the folder again. Leaving the impression that no folder was opened.

on opening folder this_folder

tell application "Finder"

activate

tell application "Finder" to close front window

display dialog "Safari Toolbar Button" -- Your action script goes here

end tell

end opening folder

Save the handler in your Users ~/Library/Scripts/Folder Action Scripts/mySafariButton.scpt Right click "Folder Action Script". Choose services from the contextual menu. And choose Folder Actions Setup. Attach your handler script. Open a Finder window and locate your handler script. Simply draw it to the Safari Favorites Bar. Alternatively create an alias of the script and move it to a new Folder. Attach a Folder action(same procedure as above) rename the alias if you like.

NOTE! Avoid folders on your desktop. Known to cause errors when working with Folder Actions. If you move the alias or the containing folder. The bookmark is broken and you have to recreate it.

share|improve this answer
    
This solution does work. But if you've opened this window's "container window" previously, Spaces switches to its space, runs the script, but unfortunately doesn't return to the space you started on ... Placing an applescript in Automator as service is really simple. – clemsam lang Dec 6 '15 at 16:08
    
I just changed my mind: By hiding the dummy folder some levels deep, the situation mentioned above will hardly EVER occur. I made a pseudo button just below Safari's Download-button that calls a script to display all my (currently listed) downloads in a "long form" as Finder's dialog: [ (⬇︎) ] – clemsam lang Dec 6 '15 at 16:48

I researched a way to "Launch Scripts from Webpage Links" on a page at Macosxautomation.com that sounds highly promising, even if it's a bit TOO thorough for my taste ...

Applescripts CAN be launched from links and therefore CAN be added as buttons on Safari's favorites-bar (NOT: toolbar), roughly by:

  • ... preparing ScriptEditor by adding XML keys/values to its internal info.plist file

  • ... writing a/some applescript/s to be applied to (a) page/s

  • ... creating a link with (calling) arguments to identify which one script to run

  • ... placing this link as button on your favorites toolbar


As I said, this sounds very promising. I'll test the scheme and report on my experience later ...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.