How to transcribe an AppleScript

applescriptautomatorcopy/paste

I found an AppleScript example on a website that I want to use, and the example is rendered as text in the browser (vs., say, being a suitably formatted file to download).

To use this AppleScript, I'm assuming that I need to use Automator somehow to create a Workflow (or perhaps an Application), but I can't figure out the right way to paste the AppleScript (or otherwise enter free-form text) into Automator.

I tried dragging a Run AppleScript action into the workflow window and then pasting the the AppleScript there, and (after I cleaned-up the smart quotes) I was able to use the Run button to execute it from within Automator. Though it popped up a warning about not receiving input while running inside Automator, the AppleScript seemed to execute correctly (i.e., I ended up with the right result). But it seems like this might not be the correct/efficient way to do this transcription (and/or it will result in an inefficient AppleScript).

Is there some way to open a free-form text window in Automator to type in an AppleScript? Or is there some other way (other than trying to figure out each Action to drag into the workflow window and modify accordingly) to transcribe an AppleScript?

Corroborating my suspicions of inefficiency, I see that the resulting Application bundle/directory has a file called Contents/document.wflow and within that is the free-form pasted text as an ActionParameters member of Run AppleScript — and this presumably will need to be parsed at every runtime.

I'm using Automator 2.7 on MacOS 10.12.6

Best Answer

You might be making this more complicated than it needs to be. If you want the code from the other site in an applescript, that page provides guidance. You don't need Automator to simply run that script.

Copy the script text onto the clipboard. Launch the Script Editor. It should open a new and empty document. Paste the copied script. The text should appear as whatever your default is for new (or uncompiled) text, e.g. probably in a purple monospaced font.

Confirm that everything is hunky dory by compiling the script: Type Command-K (or from the menus: Script > Compile). It should take on the same appearance as what you saw on the other site, e.g. some words coloured or bolded (emboldened?). Depending upon whatever, you may find that the quotation marks have been altered and you get an error. They need to be straight double quotation marks, e.g. "" and not “”.

Then try it out. If it works as desired, save it somewhere easy to access, e.g. the Safari scripts folder. Then, if you have the Script menu enabled (check the box in the Script Editor General preferences), you will be able to run the script from the script icon that shows up on the right end of the menu bar (near the clock) whenever Safari is the active app.

/Users/username/Library/Scripts/Applications/Safari

NB I'd suggest saving it as a script (as opposed to application) unless you have a specific need that requires that.