I thought I’d share a very simple script I came up with that is making my life a lot easier. Forgive me if this is old news, but I find I often get the notion to write a script for an application while I’m using it. If you are using the Apple Script Menu (or my replacement: http://www.red-sweater.com/RedSweater/FastScripts.html then this handy script will let you open the dictionary of whatever app you’re running.
It’s fast and convenient!
tell application “Finder”
set frontApp to first application process whose frontmost is true
end tell
tell application “Script Editor”
activate
open file of frontApp as alias
end tell
Has anyone else seen this problem? When I run the script from Apple’s script menu, it throws Script Editor into a state of dumbness (it tries to do something but all I get is the spinning beachball). After force quitting Script Editor, I modified the script to use Script Debugger and it attempted to open the dictionary of System Events, even when another app was frontmost. SD went dumb too.
Running the script from FastScripts works fine. I think the problem is that Apple’s script menu always thinks that System Events is the frontmost app when it is used to run a script. This has been discussed on the applescript-users list and I hope that Apple considers it a bug.
FYI: OS X 10.2.6 - Script Editor 2 - Script Debuger 3.0.6 - System Events beta (UI Scripting)
I think it’s time to buy FastScripts. What a sneaky way to force a purchase!
That error has something to do with the fact that Apple’s Script Menu uses System Events to operate, so it’s calling against itself and it “blows up real good”. You can run this script from the Finder without a hitch, but if you run it from the Script Menu it will lock up the Script Editor…
tell application "Script Editor" to open alias ":System:Library:CoreServices:System Events.app:"
Whereas the above script will work just fine for any other application, from the Script Menu. Go figure…
Here’s a modified version of the script for Script Debugger. The first tell block uses System Events instead of the Finder because, according to those in the know, the Finder hands off the ‘process’ commands to System Events anyway (at least in Jaguar). This eliminates using the Finder as a middleman and may result in a speed gain measured in milliseconds. Like the script that started this thread, it works only with FastScripts and not Apple’s script menu.
tell application "System Events"
set frontApp to first application process whose frontmost is true
end tell
tell application "Script Debugger"
activate
open file of frontApp as alias
end tell
For more fun and excitement, and for those who don’t have the FastScripts menu, save the following script as a stay-open app and then launch it. When launched, it will do nothing. After that, clicking the icon in the Dock will open the dictionary of the frontmost app in Script Debugger. It could easily be adapted for Script Editor.
on run -- Execute this code when the script is launched.
-- As written, the script will do nothing except sit running in the Dock
-- when first launched .
-- We don't want the script to be frontmost after it is launched. The
-- following line will take care of this.
tell application "System Events" to ¬
set visible of (item 1 of (every process whose frontmost is true)) to false
end run
on reopen -- Execute this code when the script is running and its Dock icon is clicked.
-- As in the run hundler, we don't want the script to be frontmost. This will
-- take care of that and then get the process which was frontmost before the
-- script's Dock icon was clicked.
tell application "System Events"
set visible of (item 1 of (every process whose frontmost is true)) to false
set frontApp to first application process whose frontmost is true
end tell
-- Bring Script Debugger to the front and open the app's dictionary.
tell application "Script Debugger"
activate
open file of frontApp as alias
end tell
end reopen
Daniel (redsweater), if you like, feel free to include these if you ever update the script submitted to ScriptBuilders. Written and tested using OS X 10.2.6 and Script Debugger 3.0.6.