Have you experienced problems running some Applescript applications in Mavericks or Yosemite? I certainly have, and I'm told about it by a dialog box that looks something like this:
In older systems, like Snow Leopard, all you had to do was check a box in "System Preferences -> Universal Access" to "Enable assess for assistive devices". But, what I've discovered is that Apple redesigned "Security & Privacy" in "System Preferences", and in particular now requires a list of "accepted" applications in their Privacy -> Accessibility panel. The list's title in Yosemite is "Allow the apps below to control your computer". That comes preloaded with a few Apps, one of which may be "applet", which gives the impression an Applescript application should work, but both applets and droplets are strictly forbidden in the list. You're supposed to be able to drag & drop an application into the (unlocked) list. but it never appears there. There's a lot about this on the web, with very few solutions. There was a suggestion the Automator could be used instead of Applescript, so I explored that approach. I have several Applescripts that I've turned into applications, and I didn't want to start over. The suggestion went on to say "use Automator" and "Run Applescript", and sure enough, that's the solution. Here's what you have to do to convert your Applescript into Automator applications that basically run your (almost) original Applescript.
First, make sure your existing Applescript, recompiled and replaced as an Application on your system gets the dialog above. If not, keep what you've got for that Application. Otherwise, if your failing Applescript doesn't have an "on run" to "end run" block around the main code, you need to add that block:
global variables, listform -- if you have any
on run {}
-- your main code goes here --
end run
-- optionally followed by your other on processes --
on process (possible parms)
-- your process code goes here --
end process
That's the basic layout. For me, it just meant inserting the "on run" and "end run" around my main code. I've attached a sample "Script.applescript" to this page to demonstrate the concepts. Notice the "my timer" statements and the "on timer" process. This was necessary to fix a bug in test running the script in Automator, where "delay" statements didn't delay properly.
One necessary step is to enable UI elements in "System Events". That is done in "on checkUI" called by "my checkUI". If your application isn't UI-compliant, you are told "UI disabled", and then you're likely to get the dialog above. So what can you do about this? First, make sure you do this on your local machine, NOT via remote-login. This is my prescription:
A) Make sure your Applescript follows the layout shown above, with the "on run" through "end run" block.
B) Save your Applescript as a "filename.applescript" file, which means if it's already a ".scpt" or ".scptd" file, you'll need to convert it by double-clicking it to get it into either AppleScript Editor (Mavericks), or Script Editor (Yosemite) where you can then "File -> Export..." and choose "Text" as the format because .applescript files are simply text files. Save your source as an "applescript" text file.
C) From within Applications -> Utilities -> Terminal.app, navigate (cd) to where your ".applescript" file is stored.
Then do this simple command: pbcopy < your.applescript
replacing "your" by the actual filename of your Applescript. This will copy the text file to your Clipboard.
D) Launch "Automator" from the "Applications" directory.
E) Click "File -> New" to get a "choose a type for your document" page, click on "Application", and click "Choose".
F) You should see an entry box with the word "Name" in dim-characters. Click in the box and type "Run".
G) One of your choices should be "Run AppleScript". Click & hold that, and drag it into the right panel.
H) You should then see a "Run AppleScript" panel with a box containing a skeleton version of "on run" through "end run".
I ) Click anywhere in that text, and do "Cmd-A" (Select All in the Edit menu). This highlights the code block.
J ) I then do "Cmd-V" (Paste from the Edit menu), which replaces the highlighted code.
K) Your Clipboard (from step C) should have been copied into the text box. Click on the "hammer" icon (compile).
L) "File -> Export..." gets you to another page where you get to save your Automator Application.
M) Make sure the "Format Type" selection says "Application". If not, make it so.
N) You should see an "Untitled.app" next to "Export As:", and a list of "Favorites" on the left side.
O) Type over the "Untitled" with the name you wish to give your application. I don't recommend having blanks in the name.
P) Click on the Favorite where you want you application to be stored. I typically use either Applications or Desktop.
Q) Click the "Save" button at the lower right. If your application already exists, you'll be asked to replace it.
R) You can now Quit from Automator, without saving.
That's basically it for Automator, but you're NOT done. Anytime you save an Automator Application that you want to run with "UI elements enabled", you need to ADD the application to the following: "System Preferences -> Security & Privacy -> Accessibility" (SPSPA). To do that, use the Apple menu and navigate to SPSPA, NOT to System Preferences -> Accessibility. Click on the lock at the bottom left, and enter your administrator password to gain update access. If your application already exists in the list, click (once) on it to select it, and then click the [-] option below. That will remove the old version. Now, click the [+] option, and a Finder-style window will appear that allows you to navigate to where your Automator Application is stored. "Open" it to ADD it to the Accessibility panel. Make sure the check-box in front of the name is checked. I added several Applications of general use, such as: a "browser", Automator, and from Utilities, both Terminal.app and Script Editor.app, You can then Quit from System Preferences. You may have to Logout and Login again to activate everything properly.
Your application should now run properly, and you can launch it in several ways. You can directly double-click it from a Finder window. You can run it from within a Terminal.app session using a simple script such as:
#!/bin/bash
open /path/to/your.app >/dev/null 2>&1
exit 0
The "open" command executes the "on run" block in your application. You can even program a "crontab" entry to allow your application to run on a schedule, as long as the computer is awake. If you know how, you could code a "launchd" plist using something like LaunchControl.app (buy-it-ware), or by creating your own plist in /Library/LaunchDaemons, with associated executable code.
OK, but what about making changes to your application, like the "username" in my Script.applescript? You don't want to go through the long procedure above every time. Fortunately, there's a simpler way. But, be forewarned, you should delete this version from SPSPA. Then, just launch Automator, and click "File -> Open" to open your stored application. Yes, you read that correctly. Your executable application contains the applescript code, so when Automator opens it, you'll see the Run Applescript window similar to step H above. You can edit your applescript right in that window.
Before you make changes, you might want to preserve your current code, There are different methods depending upon your version of Automator. You can tell which version you have by looking at its "File" menu. The older versions have "Save As..." which is simple to use. Just select "Save As...", enter a name for your backup and where it should be stored, and the File Format, and save it. You can then use "File- > Open Recent" to reopen your original code.
The newer versions have "File -> Duplicate" which creates a second window with a title saying it's a "copy". Click "File -> Save" to save the copy, but you'll probably want to change the filename, and choose from "Favorites" where to save it. You can even select the File Format. Then click the "Save" button on that page. Finally, close the window (Cmd-W) to return to your original window.
You can edit your original code, and when done, click the "hammer" to compile it, and then click "File -> Save" to replace your application. Now you must revisit SPSPA, and add [+] your new version to re-enable assistive devices. Lastly, if things go wrong, have Automator "File -> Open" your backup (either application or workflow) and proceed from there by treating this as your original code, for which a new backup will be your corrupted application..
IMPORTANT WARNING: If your application already exists, always remove the existing entry in SPSPA first, then make changes, recompile, and replace your application. When done, return to the SPSPA, click the [+] button to navigate to where your new version exists, and Open it to add it. SPSPA records more than the name of the application; it records mod date/time, so you must delete-old, add-new.
Finally, there may be a bug in SPSPA whereby your added application may be prefixed by "old." even though it's your new version. It still seems to work, but I don't understand why my "DSLExt.app" appears as "old.DSLExt.app".