Tuesday, August 11, 2020

#26 2020-01-11 07:37:56 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 139

Re: AppleScript Dictionary Script

I made a library a while ago that dealt with this sort of thing, so I dug out a couple of handlers from it and checked it still works (in High Sierra).  Despite my undying protestations against using Finder for anything AppleScript related except telling it to quit, I actually elected to use it for this task because it can enumerate a folder structure recursively with entire contents (if one knows how to use it optimally—I wrote a post about this a while ago outlining the principles of this), and because it allows one to enumerate scriptable applications that need not be running processes: every application file class object in Finder has the has scripting terminology property too.

On my clunky machine, the ScriptableApplications handler enumerates all scriptable applications within my /Applications directory in zero seconds, and does the same for /System/Library/CoreServices folder.  These are the only two I've used it on, as they're the only two that contain applications of any value to the user.  If you want it to enumerate the entire hard drive, then you deserve what ensues (just don't).

The ScriptingDictionary handler accepts a bundle identifier for the scriptable application for which you want to retrieve its .sdef file to open in Script Editor.  There's no error handling incorporated into this, so it's not idiot-proof.  If you pass it an application name, or a bundle identifier for an application that isn't scriptable, it will throw an error.  Like a lot of things, failure is the worst case scenario when calculating efficiency of an algorithm, as it only knows it has failed after checking every possibility.  But if you pass it an option it can succeed with, it'll do so reasonably quickly (about a second, maybe two).  It only searches for .sdef files currently, so it will actually fail trying to get Script Editor's dictionary file.

Applescript:

property directory : path to applications folder

use Finder : application id "com.apple.Finder"
property folder : a reference to folder (a reference to directory)
property contents : a reference to (my folder)'s entire contents
property list : missing value

on ScriptableApplications at path
       local path
       
       set my directory to the path as POSIX file
       set my list to a reference to application files in its contents
       
       script
               property parent : my list
               property id : my id
               property key : my has scripting terminology
       end script
       
       tell the result
               repeat with A in (a reference to id)
                       set [[x], key] to key's [it, rest]
                       if not x then set A's contents to false
               end repeat
               
               return id's text
       end tell
end ScriptableApplications

on ScriptingDictionary for bundleid
       local bundleid
       
       set my directory to folder "Contents:Resources:" in ¬
               application file id bundleid as alias
       set my list to a reference to files in its contents
       set my text item delimiters to linefeed
       
       script
               property parent : my list
               property name : my name
               property key : my name extension as text
       end script
       
       tell the result
               set my text item delimiters to "sdef"
               set i to the number of paragraphs in the key's first text item
               set f to item i of the name
               if f does not end with ".sdef" then return false
               return file f in the folder directory as alias
       end tell
end ScriptingDictionary

return ScriptableApplications at "/Applications"
tell application id "com.apple.ScriptEditor2" to open my (ScriptingDictionary for "com.apple.Reminders")

Whether this will work or not in Catalina is not something I'll be in a rush to attend to, as I don't use Catalina except for development, and I use Swift to do this job now.  Although, I also just found this handler I wrote in AppleScriptObjC but never really used it:

Applescript:

to readSDEF for id
       local id
       
       (NSString's stringWithContentsOfFile:(firstObject() in ((NSBundle's ¬
               bundleWithIdentifier:id)'s pathsForResourcesOfType:("sdef") ¬
               inDrectory:(missing value)))) as text
end readSDEF

You'd need to add suitable headers to import Foundation and declare/define the two classes, but that can obviously replace the vanilla handler above very easily.

Model: MacBook 12" 2016
AppleScript: 2.7
Operating System: macOS 10.13

Offline

 

#27 2020-01-11 10:14:06 pm

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

Re: AppleScript Dictionary Script

But not all scriptable apps have an sdef file.


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

Offline

 

#28 2020-01-12 03:59:01 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4567

Re: AppleScript Dictionary Script

peavine wrote:


I tried your revised script and encountered a delay as before. I suspect this is something I'm doing wrong.




May you test the script from the Script Editor ?

Here I got:

Applescript:


Don't click upon [Open this Scriplet in your Editor:] this is a log history

tell application "System Events"
   get application process 1 whose frontmost = true
       --> application process "Script Editor"
   get name of application process "Script Editor"
       --> "Script Editor"
   get has scripting terminology of application process "Script Editor"
       --> true
   get path of file of application process "Script Editor"
       --> "SSD 1000:Applications:Utilities:Script Editor.app:"
   get name of every menu item of menu 1 of menu bar item 9 of menu bar 1 of process "Script Editor"
       --> {"Placer dans le Dock", "Placer toutes les fenêtres dans le Dock", "Réduire/agrandir", "Réduire/agrandir toutes les fenêtres", missing value, "Afficher l’onglet précédent", "Afficher l’onglet suivant", "Placer l’onglet dans une nouvelle fenêtre", "Fusionner toutes les fenêtres", missing value, "Tout ramener au premier plan", "Mettre au premier plan", missing value, "Historique", "Bibliothèque", missing value, "Enregistrer comme réglage par défaut", missing value, "Sans titre", "Sans titre 2", "Sans titre 3", "Sans titre 4", "search in lProjs.scpt", "set windows settings.applescript"}
end tell
tell application "Script Editor"
   open file "SSD 1000:Applications:Utilities:Script Editor.app:"
       --> missing value
end tell
tell current application
   activate
end tell

As you see, they were not "Script Editor.sdef" window open so the script opened the file.

Applescript:


Don't click upon [Open this Scriplet in your Editor:] this is a log history

tell application "System Events"
   get application process 1 whose frontmost = true
       --> application process "Script Editor"
   get name of application process "Script Editor"
       --> "Script Editor"
   get has scripting terminology of application process "Script Editor"
       --> true
   get path of file of application process "Script Editor"
       --> "SSD 1000:Applications:Utilities:Script Editor.app:"
   get name of every menu item of menu 1 of menu bar item 9 of menu bar 1 of process "Script Editor"
       --> {"Placer dans le Dock", "Placer toutes les fenêtres dans le Dock", "Réduire/agrandir", "Réduire/agrandir toutes les fenêtres", missing value, "Afficher l’onglet précédent", "Afficher l’onglet suivant", "Placer l’onglet dans une nouvelle fenêtre", "Fusionner toutes les fenêtres", missing value, missing value, "Tout ramener au premier plan", "Mettre au premier plan", missing value, "Historique", "Bibliothèque", missing value, "Enregistrer comme réglage par défaut", missing value, "Sans titre", "Sans titre 2", "Sans titre 3", "Sans titre 4", "Script Editor.sdef", "search in lProjs.scpt", "set windows settings.applescript"}
   click menu item "Script Editor.sdef" of menu 1 of menu bar item 9 of menu bar 1 of process "Script Editor"
       --> menu item "Script Editor.sdef" of menu "Fenêtre" of menu bar item "Fenêtre" of menu bar 1 of application process "Script Editor"
end tell

This time,
As you see, a window "Script Editor.sdef" was available so the script just click the menu item bringing the window at front.

Of course, if the dictionary of a given application doesn't appear in a xx.sdef window the scheme will not apply.
If you meet such case, it would be fine to post the name of the window displaying the dictionary.
It would be quite easy to enhance the script accordingly.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 12 janvier 2020  10:57:48

Offline

 

#29 2020-01-12 08:48:08 am

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

Re: AppleScript Dictionary Script

Yvan. When you provided your script in Post 20, I was busy working on my own script, and I was more than a little frustrated that I couldn't get it to work. You took the time to help, and I was negligent not to spend some additional time on this.

I did some troubleshooting with your script in Post 20 and discovered why it didn't work as expected. I have Finder set *not* to show extensions and so the script did not find:

appName & ".sdef"



I changed the setting in Finder to show extensions and now the script works perfect. Many thanks for the help.

Last edited by peavine (2020-01-12 10:07:39 am)


2018 Mac mini - macOS Catalina

Offline

 

#30 2020-01-12 10:46:40 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4567

Re: AppleScript Dictionary Script

peavine wrote:


I changed the setting in Finder to show extensions and now the script works perfect. Many thanks for the help.



As I always display the extensions, I never think that other users may apply an other setting.

I edited message #20.
Now the script check for a menu item "appName" and for a menu item "appName.sdef" wink

Of course there is always the possible problem of applications whose dictionary open in a window named differently.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 12 janvier 2020  17:45:06

Offline

 

#31 2020-01-13 11:02:01 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 139

Re: AppleScript Dictionary Script

Shane Stanley wrote:

But not all scriptable apps have an sdef file.

I know.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)