Quick-Open AppleScript Dictionary of any running application

As I think, the “Open Dictionary…” command of Script Editor is really slow, because it will scan for all applications you have installed, I thought about a quicker way to do it. And because most of the times when I’m scripting something, the application I want to script is already running, this script (preferable as a script-menu item) will just give you a dialog to choose the running application, whose dictionary you want to access. It also includes the “standard additions”.

If you think, this script needs improvement, just give you 2 cents… :slight_smile:



tell application "Finder" to set all_apps to name of processes whose has scripting terminology is true

copy "-> Standard Additions" to the end of all_apps
set choosen to choose from list all_apps with prompt "Choose Application to show its AS Dict"

if choosen ≠ false then
	tell application "Script Editor" to activate
	if (item 1 of choosen) ≠ "-> Standard Additions" then
		tell application "Finder" to set path_to_app to file of (item 1 of (processes whose name = (item 1 of choosen)))
		tell application "Script Editor" to open path_to_app
	else
		tell application "Finder" to open file "Standard Additions" of folder "ScriptingAdditions" of folder "Library" of (path to current user folder)
	end if
end if


Model: PowerMac G5 2.3GHz Dual
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Excellent idea.

I’ve always been annoyed by how slow the AS dictionaries open, and yes, the application whose dictionary I need is usually running.

Oddly, when I compared the time difference Script Editor’s “Open Dictionary” was not nearly as slow as usual - not even slow enough to be a nuisance. I wonder if the fact that I just moved all third party apps into an application folder I set up in my home folder, defragged with Drive Genius, and rebuilt my launch services database had any affect on that.

You script is still a little faster (and any fiddling I did slowed it down) so it will still come in handy.

Thanks,

j

Edit: typo

Hi, CTE.

The filter in the first line doesn’t work in Jaguar because of a bug in that system, but it’s fine in Tiger.

The StandardAdditions file is called “StandardAdditions.osax” and lives in the system ScriptingAdditions folder “ unless you’ve made other arrangements on your own machine. There is a path to destination for the ScriptingAdditions folders, so you could simply write:

tell application "Finder" to open file "StandardAdditions.osax" of (path to scripting additions)

-- or:
tell application "Finder" to open file "StandardAdditions.osax" of (path to scripting additions from system domain)

For the user’s Scripting Additions folder, the equivalent would be:

tell application "Finder" to open file "SomeOsax.osax" of (path to scripting additions from user domain)

But if you wanted to use a Finder reference, the Finder has its own keyword for the current user folder: home.

tell application "Finder" to open file "SomeOsax.osax" of folder "ScriptingAdditions" of folder "Library" of home

Otherwise, of course, a great idea. I have to say that because I’ve been using something similar myself for years. :wink: If you’re interested, it’s here in the ScriptBuilders section of this site.

For running applications, an alternative technique is to command drag the application icon in the Dock onto the Script Editor icon.

You’re welcome! But I have to admin, that I like Nigel’s script(s) even better. You may want to check them out… < HERE >

Thanks for this line. That is much more convinient! :slight_smile:

I really like these scripts! Well done.

Well, that is interesting and will come in handy every now and then. But it doesn’t work with Finder itself. I think, that had been my original intention to write the script at all… :wink:

Thank you for all for your comments!

i tried to use it, but all it does is open a finder window and select the app… what should happen?

I edited the Script by the CreaTurE in post #1, I edited it slightly as Script Editor has changed name to AppleScript Editor in the mean time. I am happy to say that it works as it should, and sorry that Nigel’s are unavailable!

It opens the dictionary for the app of your choice in AppleScript Editor!



-- http://macscripter.net/post.php?tid=18628 
tell application "Finder" to set all_apps to name of processes whose has scripting terminology is true

copy "-> Standard Additions" to the end of all_apps
tell application id "sevs" to set curn to (name of first item of (every application process whose frontmost is true))

tell application "SystemUIServer"
	activate
	set choosen to choose from list all_apps with prompt "Choose Application to show its AS Dict" default items item 1 of all_apps
end tell
tell application curn to activate

if choosen ≠ false then
	tell application "AppleScript Editor" to activate
	if (item 1 of choosen) ≠ "-> Standard Additions" then
		tell application "Finder" to set path_to_app to file of (item 1 of (processes whose name = (item 1 of choosen)))
		tell application "AppleScript Editor" to open path_to_app
	else
		tell application "Finder" to open file "Standard Additions" of folder "ScriptingAdditions" of folder "Library" of (path to current user folder)
	end if
end if

Edit

Did a little bit more to make it look a little bit better. :slight_smile:

I got fed up with the slowness of mine on my MacBook Pro, so I rewrote them from scratch in January for Tiger or later only. I never got round to uploading them. Here they are. By the way, the rather lame name “Open Dictionaries” isn’t mine. It was given to an early version of the Running Apps script by a MacScripter administrator who transfered it to ScriptBuilders from some earlier feature of this site. Script of the Week, I think.

Running Apps:

(* Dictionaries - Running Apps (Open Dictionaries 2.2.2), by Nigel Garvey, January 2012  *)
main()

on main()
	tell application "Finder" to set sysv to (system attribute "sysv")
	if (sysv < 4160) then abort("This version of the script only supports Mac OS X 10.4.0 or later.")
	
	set editorName to getEditorName()
	openDictionaries(editorName)
end main

on getEditorName()
	-- Check that either Script Editor, Smile, or Script Debugger is running.
	tell application "System Events" to set {editorNames, editorFrontmosts} to {name, frontmost} of (every application process whose creator type is "ToyS" or creator type is "VIZF" or creator type is "asDB")
	
	set editorCount to (count editorNames)
	if (editorCount is 0) then
		my abort("Please open (Apple)Script Editor, Smile, or Script Debugger before using this script.")
	else if (editorCount is 1) then
		-- One of the editors is running.
		set editorName to beginning of editorNames
	else if (editorFrontmosts contains true) then
		-- More than one of the editors is running. If one is the frontmost app, use that.
		repeat with i from 1 to editorCount
			if (item i of editorFrontmosts) then
				set editorName to item i of editorNames
				exit repeat
			end if
		end repeat
	else
		-- Otherwise consult the user for which editor to use.
		tell application (path to frontmost application as Unicode text) to set theChoice to (choose from list editorNames with prompt "In which editor do you want to view the dictionaries?" with title "Dictionaries - Running Apps")
		if (theChoice is false) then error number -128
		set editorName to beginning of theChoice
	end if
	
	return editorName
end getEditorName

on abort(msg)
	tell application (path to frontmost application as Unicode text) to display dialog msg buttons {"Cancel"} default button 1 with icon stop cancel button 1 with title "Dictionaries - Running Apps"
end abort

on openDictionaries(editorName)
	set specialNames to {"Finder"}
	
	tell application "System Events" to tell (application processes whose has scripting terminology is true and name is not "Image Capture Scripting") to set {procNames, procFiles} to {its name, its file}
	
	specialSort(procNames, procFiles, specialNames)
	
	tell application (path to frontmost application as Unicode text) to set theChoice to (choose from list procNames default items specialNames with prompt "Which dictionaries do you want to open?" with title "Dictionaries - Running Apps" with multiple selections allowed)
	if (theChoice is false) then error number -128
	
	set theFiles to {}
	repeat with i from 1 to (count procNames)
		if (item i of procNames is in theChoice) then set end of theFiles to item i of procFiles
	end repeat
	
	tell application editorName to open theFiles
end openDictionaries

on specialSort(procNames, procFiles, specialNames)
	-- This script customises the sort to group any "special" application names before the others. It does this by judging non-special names to have higher comparison values.
	script specialsFirst
		property specials : specialNames
		
		on isGreater(a, b)
			set |b's special| to (b is in specials)
			if ((|b's special|) = (a is in specials)) then
				(a > b)
			else
				(|b's special|)
			end if
		end isGreater
	end script
	
	-- This script duplicates in procFiles moves performed in procNames by the custom insertion sort.
	script parallel
		property lst : procFiles
		
		on shift(a, b)
			tell item b of my lst
				repeat with i from b - 1 to a by -1
					set item (i + 1) of my lst to item i of my lst
				end repeat
				set item a of my lst to it
			end tell
		end shift
	end script
	
	CustomInsertionSort(procNames, 1, -1, {comparer:specialsFirst, slave:parallel})
end specialSort

-- Custom insertion sort from A Dose of Sorts by Nigel Garvey.
on CustomInsertionSort(theList, l, r, customiser)
	script o
		property comparer : me
		property slave : me
		property lst : theList
		
		on isrt(l, r)
			set u to item l of o's lst
			repeat with j from (l + 1) to r
				set v to item j of o's lst
				if (comparer's isGreater(u, v)) then
					set here to l
					set item j of o's lst to u
					repeat with i from (j - 2) to l by -1
						tell item i of o's lst
							if (comparer's isGreater(it, v)) then
								set item (i + 1) of o's lst to it
							else
								set here to i + 1
								exit repeat
							end if
						end tell
					end repeat
					set item here of o's lst to v
					slave's shift(here, j)
				else
					set u to v
				end if
			end repeat
		end isrt
		
		-- Default comparison and slave handlers to customise for an ordinary sort.
		on isGreater(a, b)
			(a > b)
		end isGreater
		
		on shift(a, b)
		end shift
	end script
	
	-- Process the input parmeters.
	set listLen to (count theList)
	if (listLen > 1) then
		-- Negative and/or transposed range indices.
		if (l < 0) then set l to listLen + l + 1
		if (r < 0) then set r to listLen + r + 1
		if (l > r) then set {l, r} to {r, l}
		
		-- Supplied or default customisation scripts.
		if (customiser's class is record) then set {comparer:o's comparer, slave:o's slave} to (customiser & {comparer:o, slave:o})
		
		-- Do the sort.
		o's isrt(l, r)
	end if
	
	return -- nothing.
end CustomInsertionSort

OSAXen:

(* Dictionaries - OSAXen (Open Dictionaries 2.2.2), by Nigel Garvey,  January 2012  *)
main()

on main()
	tell application "Finder" to set sysv to (system attribute "sysv")
	if (sysv < 4160) then abort("This version of the script only supports Mac OS X 10.4.0 or later.")
	
	set editorName to getEditorName()
	openDictionaries(editorName)
end main

on getEditorName()
	-- Check that either Script Editor, Smile, or Script Debugger is running.
	tell application "System Events" to set {editorNames, editorFrontmosts} to {name, frontmost} of (every application process whose creator type is "ToyS" or creator type is "VIZF" or creator type is "asDB")
	
	set editorCount to (count editorNames)
	if (editorCount is 0) then
		my abort("Please open (Apple)Script Editor, Smile, or Script Debugger before using this script.")
	else if (editorCount is 1) then
		-- One of the editors is running.
		set editorName to beginning of editorNames
	else if (editorFrontmosts contains true) then
		-- More than one of the editors is running. If one is the frontmost app, use that.
		repeat with i from 1 to editorCount
			if (item i of editorFrontmosts) then
				set editorName to item i of editorNames
				exit repeat
			end if
		end repeat
	else
		-- Otherwise consult the user for which editor to use.
		tell application (path to frontmost application as Unicode text) to set theChoice to (choose from list editorNames with prompt "In which editor do you want to view the dictionaries?" with title "Dictionaries - Running Apps")
		if (theChoice is false) then error number -128
		set editorName to beginning of theChoice
	end if
	
	return editorName
end getEditorName

on abort(msg)
	tell application (path to frontmost application as Unicode text) to display dialog msg buttons {"Cancel"} default button 1 with icon stop cancel button 1 with title "Dictionaries - Running Apps"
end abort

on openDictionaries(editorName)
	tell application "System Events"
		set {systemNames, systemPaths} to {name, path} of (files of scripting additions folder of system domain where it is visible and name is not "Image Capture Scripting.app")
		set {localNames, localPaths} to {name, path} of (files of scripting additions folder of local domain where it is visible)
		set {userNames, userPaths} to {name, path} of (files of scripting additions folder of user domain where it is visible)
	end tell
	specialSort(systemNames, systemPaths)
	specialSort(localNames, localPaths)
	specialSort(userNames, userPaths)
	
	set allNames to systemNames & localNames & userNames
	set allPaths to systemPaths & localPaths & userPaths
	
	set chooseList to insertDivider(systemNames)
	if (localNames is not {}) then set chooseList to chooseList & separator() & insertDivider(localNames)
	if (userNames is not {}) then set chooseList to chooseList & separator() & insertDivider(userNames)
	
	tell application (path to frontmost application as Unicode text) to set theChoice to (choose from list chooseList with prompt "Which OSAXen and/or scripting application dictionaries do you want to open?" default items {"StandardAdditions.osax"} with title "Dictionaries - OSAXen" with multiple selections allowed)
	if (theChoice is false) then error number -128
	
	repeat with i from 1 to (count allNames)
		if (item i of allNames is in theChoice) then tell application editorName to open file (item i of allPaths)
	end repeat
end openDictionaries

on insertDivider(nameList)
	if (end of nameList ends with ".app") then
		repeat with i from 1 to (count nameList)
			if (item i of nameList ends with ".app") then exit repeat
		end repeat
		set nameList to items 1 thru (i - 1) of nameList & divider() & items i thru -1 of nameList
	end if
	
	return nameList
end insertDivider

on divider()
	return "···············"
end divider

on separator()
	return "----------"
end separator

on specialSort(fileNames, filePaths)
	script specialsFirst
		on isGreater(a, b)
			if (b ends with ".osax") then
				((a ends with ".app") or (b is "StandardAdditions.osax") or ((a > b) and (a is not "StandardAdditions.osax")))
			else
				((a ends with ".app") and (a > b))
			end if
		end isGreater
	end script
	
	-- This script duplicates in filePaths moves performed in fileNames by the custom insertion sort.
	script parallel
		property lst : filePaths
		
		on shift(a, b)
			tell item b of my lst
				repeat with i from b - 1 to a by -1
					set item (i + 1) of my lst to item i of my lst
				end repeat
				set item a of my lst to it
			end tell
		end shift
	end script
	
	CustomInsertionSort(fileNames, 1, -1, {comparer:specialsFirst, slave:parallel})
end specialSort

-- Custom insertion sort from A Dose of Sorts by Nigel Garvey.
on CustomInsertionSort(theList, l, r, customiser)
	script o
		property comparer : me
		property slave : me
		property lst : theList
		
		on isrt(l, r)
			set u to item l of o's lst
			repeat with j from (l + 1) to r
				set v to item j of o's lst
				if (comparer's isGreater(u, v)) then
					set here to l
					set item j of o's lst to u
					repeat with i from (j - 2) to l by -1
						tell item i of o's lst
							if (comparer's isGreater(it, v)) then
								set item (i + 1) of o's lst to it
							else
								set here to i + 1
								exit repeat
							end if
						end tell
					end repeat
					set item here of o's lst to v
					slave's shift(here, j)
				else
					set u to v
				end if
			end repeat
		end isrt
		
		-- Default comparison and slave handlers to customise for an ordinary sort.
		on isGreater(a, b)
			(a > b)
		end isGreater
		
		on shift(a, b)
		end shift
	end script
	
	-- Process the input parmeters.
	set listLen to (count theList)
	if (listLen > 1) then
		-- Negative and/or transposed range indices.
		if (l < 0) then set l to listLen + l + 1
		if (r < 0) then set r to listLen + r + 1
		if (l > r) then set {l, r} to {r, l}
		
		-- Supplied or default customisation scripts.
		if (customiser's class is record) then set {comparer:o's comparer, slave:o's slave} to (customiser & {comparer:o, slave:o})
		
		-- Do the sort.
		o's isrt(l, r)
	end if
	
	return -- nothing.
end CustomInsertionSort

Core Services:

(* Dictionaries - Core Services (Open Dictionaries 2.2.2), by Nigel Garvey, January 2012  *)
main()

on main()
	tell application "Finder" to set sysv to (system attribute "sysv")
	if (sysv < 4160) then abort("This version of the script only supports Mac OS X 10.4.0 or later.")
	
	set editorName to getEditorName()
	openDictionaries(editorName)
end main

on getEditorName()
	-- Check that either Script Editor, Smile, or Script Debugger is running.
	tell application "System Events" to set {editorNames, editorFrontmosts} to {name, frontmost} of (every application process whose creator type is "ToyS" or creator type is "VIZF" or creator type is "asDB")
	
	set editorCount to (count editorNames)
	if (editorCount is 0) then
		my abort("Please open (Apple)Script Editor, Smile, or Script Debugger before using this script.")
	else if (editorCount is 1) then
		-- One of the editors is running.
		set editorName to beginning of editorNames
	else if (editorFrontmosts contains true) then
		-- More than one of the editors is running. If one is the frontmost app, use that.
		repeat with i from 1 to editorCount
			if (item i of editorFrontmosts) then
				set editorName to item i of editorNames
				exit repeat
			end if
		end repeat
	else
		-- Otherwise consult the user for which editor to use.
		tell application (path to frontmost application as Unicode text) to set theChoice to (choose from list editorNames with prompt "In which editor do you want to view the dictionaries?" with title "Dictionaries - CoreServices")
		if (theChoice is false) then error number -128
		set editorName to beginning of theChoice
	end if
	
	return editorName
end getEditorName

on abort(msg)
	tell application (path to frontmost application as Unicode text) to display dialog msg buttons {"Cancel"} default button 1 with icon stop cancel button 1 with title "Dictionaries - CoreServices"
end abort

on openDictionaries(editorName)
	set coreServicesFolder to ((path to library folder as Unicode text from system domain) & "CoreServices:") as alias
	
	tell application "Finder"
		set {appNames, appScriptabilities} to {name, has scripting terminology} of application files of coreServicesFolder
		set appAliases to (application files of coreServicesFolder) as alias list
	end tell
	
	repeat with i from 1 to (count appScriptabilities)
		if (item i of appScriptabilities) then
		else
			set item i of appNames to missing value
			set item i of appAliases to missing value
		end if
	end repeat
	set appNames to appNames's every Unicode text
	set appAliases to appAliases's every alias
	
	tell application (path to frontmost application as Unicode text) to set chosenNames to (choose from list appNames with prompt "Which CoreService dictionaries do you want to open?" with title "Dictionaries - CoreServices" with multiple selections allowed)
	if (chosenNames is false) then error number -128
	
	repeat with i from 1 to (count appNames)
		if (item i of appNames is in chosenNames) then tell application editorName to open item i of appAliases
	end repeat
end openDictionariess

Edit: Replaced the em dashes in the opening comments with ordinary dashes. The compiler doesn’t seem to like them in single-line (* . *) comments. Weird. :confused:

Thanks a lot.

Great time-savers! :cool: