Tuesday, August 4, 2020

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

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

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: 5256

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: 563

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: 563

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: 5256

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: 6433

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: 5256

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: 6433

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: 563

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-03-08 09:20:44 am

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

Re: AppleScript Dictionary Menu

The following script is the same as that in Post 1 except it remembers the last-selected dictionary. The script does this by saving a 1-line text file in the user's preferences folder.

Applescript:


set defaultApps to {"Script Editor", "StandardAdditions", "System Events"}
set settingFile to (path to preferences as text) & name of me & ".txt"
set defaultItem to readSetting(settingFile)

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

set appList to sortList(activeApps & defaultApps)

if defaultItem is not in appList then set defaultItem to item 1 of appList

choose from list appList with title "AppleScript" with prompt "Select a dictionary to open:" default items {defaultItem} 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

writeSetting(settingFile, selectedApp)

delay 0.5 # this delay seems necessary for the script to operate correctly

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

on readSetting(theFile)
   
   try
       return paragraph 1 of (read file theFile)
   on error
       return missing value
   end try
   
end readSetting

on writeSetting(theFile, theSetting)
   
   try
       set openedFile to open for access (file theFile) with write permission
       set eof of openedFile to 0
       write theSetting to openedFile
       close access openedFile
   on error
       try
           close access openedFile
       end try
   end try
   
end writeSetting

Last edited by peavine (2020-03-26 09:05:01 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)