Monday, February 24, 2020

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

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

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.

Additional features of this script are:

* The last-selected dictionary is remembered in the dialog from one running of the script to the next.

* Selecting an already-opened dictionary will bring it quickly to the front.

* The position and size of dictionary windows can be set, and this can be modified or disabled.

I run this script by way of FastScripts. It can also be saved and run as an application, although with current versions of macOS the last-selected-dictionary feature may not work and permissions will have to be set. 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

Applescript:


main()

on main()
   
   set defaultApps to {"Script Editor", "StandardAdditions", "System Events"}
   
   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)
   
   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
           arrangeWindow(allDictionaries) of me
       end if
   end tell
   
end main

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
   tell application "Script Editor" to activate
   
end focusWindow

on arrangeWindow(allDictionaries)
   
   set {x1, y1, x2, y2} to {10, 33, 700, 800}
   set windowOverlap to 25
   
   set o to (count allDictionaries) * windowOverlap
   
   tell application "Script Editor" to set bounds of window 1 to {x1 + o, y1 + o, x2 + o, y2 + o}
   
end arrangeWindow

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-02-15 09:10:27 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: 5168

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

Online

 

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

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

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

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

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

Online

 

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

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

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

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

Online

 

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

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

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

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. It seems to be reliable but I'll continue to work on it.

Edit January 20, 2020. I've placed in Post 1 an updated and hopefully final version of my script. I've been using it for several days without encountering any errors.

Last edited by peavine (2020-01-20 09:58:55 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)