Tuesday, October 27, 2020

#1 2020-01-12 05:54:40 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 644

AppleScript Dictionary Menu

This script displays a dialog in which the user can select an AppleScript dictionary to open. The available dictionaries consist of three default dictionaries--Finder, System Events, and Standard Additions--plus dictionaries for currently-running visible apps. Additional default dictionaries such as Safari are easily added, although a very few such as Python will not work.

I run this script by way of FastScripts, but it can also be saved and run as an application. This script is designed for use with Script Editor and assumes that Script Editor is running.

I received help writing this script in the following MacScripter thread:

https://macscripter.net/viewtopic.php?id=47325

This script is a rewrite of a script originally written by Nigel Garvey:

https://www.macscripter.net/viewtopic.php?pid=191643

Applescript:


set defaultApps to {"Script Editor", "StandardAdditions", "System Events"}

tell application "System Events"
   set activeApps to name of every process whose visible is true and has scripting terminology is true
end tell

set appList to sortList(activeApps & defaultApps)

choose from list appList with title "AppleScript" with prompt "Select a dictionary to open:" default items {item 1 of appList} OK button name "Open"

if result = false then
   error number -128
else
   set selectedApp to item 1 of result
end if

if selectedApp = "StandardAdditions" then
   set appFile to ((path to scripting additions folder as text) & "StandardAdditions.osax:") as alias
else
   set appFile to path to application selectedApp
end if

tell application "Script Editor"
   set allDictionaries to name of every document whose path ends with ".sdef"
   if selectedApp is in allDictionaries then
       focusWindow(selectedApp) of me
   else if (selectedApp & ".sdef") is in allDictionaries then
       focusWindow((selectedApp & ".sdef")) of me
   else
       activate
       open appFile
   end if
end tell

on focusWindow(selectedApp)
   
   tell application "Script Editor" to activate
   tell application "System Events" to tell process "Script Editor" to perform action "AXRaise" of window selectedApp
   
end focusWindow

on sortList(unsortedList) # obtained from developer.apple.com then modified
   
   set sortedList to {}
   set indexList to {}
   set listCount to (count unsortedList)
   
   repeat listCount times
       set lowTextItem to missing value
       repeat with i from 1 to listCount
           if i is not in indexList then
               set anItem to (item i of unsortedList)
               if lowTextItem is missing value then
                   set lowTextItem to anItem
                   set lowIndexItem to i
               else if anItem < lowTextItem then
                   set lowTextItem to anItem
                   set lowIndexItem to i
               end if
           end if
       end repeat
       if lowTextItem is not in sortedList then set end of sortedList to lowTextItem
       set end of indexList to lowIndexItem
   end repeat
   
   return sortedList
   
end sortList

Last edited by peavine (2020-03-08 07:49:08 am)


2018 Mac mini - macOS Catalina

Offline

 

#2 2020-01-13 03:53:20 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5299

Re: AppleScript Dictionary Menu

Not bad!  smile

It's very similar in concept to a script I've been using myself for the past twenty years or so!  smile  I've had to rewrite mine a few times as various things have either stopped working or been reinstated over the systems, but the basic idea hasn't changed. For historical reasons, mine presents the Finder selected at the top of the choose dialog, with the other open scriptable apps sorted alphabetically below it. I have similar scripts for scripting additions and scriptable-but-not-necessarily-open applications in the CoreServices folder; but of course there's now only one OSAX dictionary worth opening and the CoreServices script keeps opening the wrong dictionaries for reasons I've never been able to determine!

When you get round to adapting yours for multiple dictionaries, you should find the windows will cascade naturally.


NG

Offline

 

#3 2020-01-13 08:27:43 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 644

Re: AppleScript Dictionary Menu

Nigel Garvey wrote:

Not bad!  smile



Nigel. Thanks for looking at my script and for the compliment.

When I began work on this project, my script operated in a manner similar to one written by Daniel, the developer of FastScripts. I then happened upon your script and was inspired to take a similar approach, which seemed more useful overall. Anyways, I make note of this and provide a link to the thread containing your script in post 25 of the MacScripter thread linked in post 1 above.

Last edited by peavine (2020-01-14 07:33:01 am)


2018 Mac mini - macOS Catalina

Offline

 

#4 2020-01-13 12:38:19 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 644

Re: AppleScript Dictionary Menu

Post deleted by peavine.

Last edited by peavine (2020-01-13 11:18:12 pm)


2018 Mac mini - macOS Catalina

Offline

 

#5 2020-01-13 04:49:45 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5299

Re: AppleScript Dictionary Menu

Hi peavine.

I get an error on the 'set bounds' line at the bottom. The names of Script Editor's dictionary windows end with ".sdef", which isn't allowed for in your handlers.


NG

Offline

 

#6 2020-01-13 05:07:01 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: AppleScript Dictionary Menu

Nigel Garvey wrote:

The names of Script Editor's dictionary windows end with ".sdef"



I think you'll find that's only if you have Show all filename extensions on in the Finder. (Actually, it probably depends on the state when SE was launched.)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#7 2020-01-13 05:22:55 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5299

Re: AppleScript Dictionary Menu

Shane Stanley wrote:
Nigel Garvey wrote:

The names of Script Editor's dictionary windows end with ".sdef"



I think you'll find that's only if you have Show all filename extensions on in the Finder.


You're right. It is. It shows you have to be careful about what assumptions you make when writing scripts.  smile


NG

Offline

 

#8 2020-01-13 05:46:04 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: AppleScript Dictionary Menu

it also supports my working hypothesis that the Finder is the root of all scripting evil wink.

More seriously, this is yet another good reason why it's best not to refer to windows by name, unless the app in question uses some kind of persistent ID as a name. It's not an issue confined to Script Editor or dictionaries.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2020-01-13 07:17:41 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 644

Re: AppleScript Dictionary Menu

Thanks Nigel and Shane.

I did the following:

* Deleted my post 4, which contained the erroneous script.

* Revised my post 1 to include an updated script, which works with dictionary names both with and without  an .sdef extension.

Last edited by peavine (2020-03-08 07:59:17 am)


2018 Mac mini - macOS Catalina

Offline

 

#10 2020-09-21 10:47:05 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 644

Re: AppleScript Dictionary Menu

The operation of this script is similar to that in post 1 above except that the user specifies dictionary names/paths in a text file. A few comments:

* The first time the script is run, it creates two small text files in the user's preferences folder.

* The user should then enter in the data file the desired dictionary names/paths (see below).

* In versions of macOS prior to Catalina, the path set in the script by editorApp will need to be changed. The script will not work properly otherwise.

Applescript:

-- Revised 2020.09.29

main()

on main()
   set preferencePath to POSIX path of (path to preferences)
   set dataFile to preferencePath & name of me & " Data.txt"
   set settingFile to preferencePath & name of me & " Setting.txt"
   set editorApp to "/System/Applications/Utilities/Script Editor.app"
   
   set {defaultSelection, dictionaryNames, dictionaryPaths} to readFile(dataFile, settingFile)
   
   set dialogList to {"Active App", "--"} & dictionaryNames & {"--", "Data File"}
   choose from list dialogList default items defaultSelection OK button name "Open" with title "AppleScript Dictionaries"
   set selectedApp to result as text
   
   if selectedApp = "false" or selectedApp = "--" then
       error number -128
   else if selectedApp = "Active App" then
       openActiveApp(editorApp)
   else if selectedApp = "Data File" then
       do shell script "open " & quoted form of dataFile
   else
       openSelectedApp(dictionaryNames, dictionaryPaths, editorApp, selectedApp)
   end if
   
   writeFile(settingFile, selectedApp)
end main

on readFile(dataFile, settingFile)
   try
       set defaultSelection to paragraph 1 of (read POSIX file settingFile)
   on error
       set defaultSelection to "Data File"
   end try
   
   set {dictionaryNames, dictionaryPaths} to {{}, {}}
   try
       set openedFile to paragraphs of (read POSIX file dataFile)
   on error
       writeFile(dataFile, linefeed & linefeed)
       errorDialog("The dictionary data file could not be found and has been created.", "Please rerun this script and add dictionary names and paths to the data file.")
       error number -128
   end try
   
   repeat with i from 1 to (count openedFile) by 2
       if item i of openedFile > "" then
           set end of dictionaryNames to item i of openedFile
           set end of dictionaryPaths to item (i + 1) of openedFile
       else
           exit repeat
       end if
   end repeat
   
   return {defaultSelection, dictionaryNames, dictionaryPaths}
end readFile

on openActiveApp(editorApp)
   try
       tell application "System Events" to set activeApp to POSIX path of (application file of (first process whose frontmost is true and has scripting terminology is true))
       do shell script "open -a " & quoted form of editorApp & space & quoted form of activeApp
   on error
       tell application "System Events" to set activeApp to name of first process whose frontmost is true
       errorDialog("Dictionary not found for " & quote & activeApp & quote, "This app may not be scriptable")
   end try
end openActiveApp

on openSelectedApp(dictionaryNames, dictionaryPaths, editorApp, selectedApp)
   repeat with i from 1 to (count dictionaryNames)
       if item i of dictionaryNames = selectedApp then exit repeat
   end repeat
   
   try
       do shell script "open -a " & quoted form of editorApp & space & quoted form of (item i of dictionaryPaths)
   on error
       errorDialog("Dictionary not found for " & quote & selectedApp & quote, "Check the path in the data file")
   end try
end openSelectedApp

on writeFile(theFile, theText)
   try
       set openedFile to open for access (POSIX file theFile) with write permission
       set eof of openedFile to 0
       write theText to openedFile
       close access openedFile
   on error
       try
           close access openedFile
       end try
   end try
   delay 0.5
end writeFile

on errorDialog(textOne, textTwo)
   display alert textOne message textTwo buttons {"OK"} default button 1 as critical
end errorDialog

Just by way of example, the following are the contents of my data file. Many of the paths may not work on versions of macOS prior to Catalina and will need to be edited.

Finder
/System/Library/CoreServices/Finder.app
Image Events
/System/Library/CoreServices/Image Events.app
Standard Additions
/System/Library/ScriptingAdditions/StandardAdditions.osax
System Events
/System/Library/CoreServices/System Events.app

Last edited by peavine (2020-09-29 08:05:17 am)


2018 Mac mini - macOS Catalina

Offline

 

#11 2020-09-22 01:00:04 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 418

Re: AppleScript Dictionary Menu

Have you not try ASDictionary.app ??

Its very easy to output dictionaries to html and later do edit inside as we want.
Who did say... that was not possible smile

https://sourceforge.net/projects/appscr … p/download


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#12 2020-09-22 07:19:08 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 418

Re: AppleScript Dictionary Menu

peavine wrote:

I couldn't find a file with that name on my computer.



Check the html link in my post.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)