You are not logged in.
The following script opens the AppleScript dictionary of the frontmost app; I run it by way of FastScripts.
Applescript:
tell application "Finder"
set frontApp to first application process whose frontmost is true
end tell
tell application "Script Editor"
activate
open file of frontApp as alias
end tell
I obtained this script from:
https://macscripter.net/viewtopic.php?pid=14764
The script works fine as long as the dictionary exists. Otherwise, the following error results and Script Editor opens an unknown document:
Unable to read the dictionary of the application or dictionary because it is not scriptable.
I added error correction to the script but that doesn't help:
Applescript:
tell application "Finder"
set frontApp to first application process whose frontmost is true
set appName to name of frontApp
end tell
try
tell application "Script Editor"
open file of frontApp as alias
activate
end tell
on error
display dialog "There is not an AppleScript dictionary for " & appName buttons {"OK"} cancel button 1 default button 1 with icon stop
end try
The source of the error appears to be the following line, but I don't understand how Script Editor can open the file of an application process.
Applescript:
open file of frontApp as alias
How can I get the error correction to work? Thanks.
Last edited by peavine (2020-01-07 10:23:21 am)
2018 Mac mini - macOS Catalina
Offline
It just occurred to me that the following script might be a more reliable approach, although I'd prefer not to have to specify every scriptable app.
Applescript:
set scriptableApps to {"Finder", "Script Editor"} -- add other app names
tell application "Finder"
set frontApp to first application process whose frontmost is true
set appName to name of frontApp
end tell
if appName is not in scriptableApps then
display dialog "There is not an AppleScript dictionary for " & appName buttons {"OK"} cancel button 1 default button 1 with icon stop
end if
tell application "Script Editor"
open file of frontApp as alias
activate
end tell
Last edited by peavine (2020-01-07 10:46:17 am)
2018 Mac mini - macOS Catalina
Offline
I guess that this fake script will help you to understand what you get :
Applescript:
tell application "Finder"
set frontApp to first application process whose frontmost is true
--> application process "Script Editor" of application "Finder"
end tell
tell application "Script Editor"
activate
set maybe to file of frontApp
log result --> (*alias SSD 1000:Applications:Utilities:Script Editor.app:*)
open maybe -- does nothing if the front app is Script Editor
end tell
set frontApp to (path to application "TextEdit") as alias
log result --> (*alias SSD 1000:Applications:TextEdit.app:*)
tell application "Script Editor"
activate
try
open frontApp -- as alias
on error errMsg number errNbr
display dialog "Error # " & errNumber & ", " & errMsg
end try
end tell
set frontApp to (path to application "LibreOffice") as alias
log result --> (*alias SSD 1000:Applications:Applications perso:LibreOffice.app:*)
tell application "Script Editor"
activate
try
open frontApp -- as alias
on error errMsg number errNbr
display dialog "Error # " & errNumber & ", " & errMsg
end try
end tell
My understanding is that the Editor doesn't send its own error to the script itself.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 7 janvier 2020 17:55:10
Offline
I guess that this fake script will help you to understand what you get :
My understanding is that the Editor doesn't send its own error to the script itself.
Thanks Yvan. I now understand the code line I didn't previously understand.
If Script Editor won't send an error to the script itself, then that would explain why the try statement is not catching the error. So perhaps specifying scriptable apps in my second post is the way to go.
2018 Mac mini - macOS Catalina
Offline
Maybe you may insert a piece of code scanning the package of the app:
Applescript:
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
use script "FileManagerLib" version "2.3.3"
----------------------------------------------------------------
script o
property theURLs : {}
end script
set theApp to path to application "Pages"
set o's theURLs to objects of theApp result type urls array with searching subfolders without include folders and include invisible items
set hasDictionary to false
repeat with aURL in o's theURLs
set theExtension to (aURL's pathExtension()) as text
if theExtension is in {"scriptTerminology", "scriptSuite", "sdef"} then
set hasDictionary to true
exit repeat
end if
end repeat
hasDictionary
I used FileManagerLib for this first draft but you may trigger the NSFileManager directly.
Applescript:
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
script o
property theURLs : {}
end script
set theApp to path to application "Pages"
set folderPath to POSIX path of theApp
set theURL to current application's class "NSURL"'s fileURLWithPath:folderPath
set fileManager to a reference to current application's NSFileManager's defaultManager()
set keysToRequest to {}
set theOptions to {current application's NSDirectoryEnumerationSkipsHiddenFiles}
set theEnumerator to fileManager's enumeratorAtURL:theURL includingPropertiesForKeys:keysToRequest options:theOptions errorHandler:(missing value)
set o's theURLs to (theEnumerator's allObjects())
set hasDictionary to false
repeat with aURL in o's theURLs
set theExtension to (aURL's pathExtension()) as text
if theExtension is in {"scriptTerminology", "scriptSuite", "sdef"} then
set hasDictionary to true
exit repeat
end if
end repeat
hasDictionary
In Pages we have a "sdef" file.
In TextEdit we have "scriptTerminology" and "scriptSuite" files but no "sdef" one.
I didn't made more extensive search.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 7 janvier 2020 19:22:32
Last edited by Yvan Koenig (2020-01-07 12:24:52 pm)
Offline
The script works fine as long as the dictionary exists.
So check if it exists:
Applescript:
tell application id "com.apple.systemevents" -- System Events.app
set scriptable to has scripting terminology of (get first application process whose frontmost is true)
end tell
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
So check if it exists:
Thanks Shane. I incorporated your suggestion in my script and it works great.
Applescript:
tell application id "com.apple.systemevents"
set frontApp to first application process whose frontmost is true
set appDictionary to has scripting terminology of frontApp
set appName to name of frontApp
end tell
if appDictionary then
tell application "Script Editor"
open file of frontApp as alias
activate
end tell
else
display dialog appName & " does not have a dictionary." buttons {"OK"} ¬
cancel button 1 default button 1 with icon stop
end if
Last edited by peavine (2020-01-09 07:49:52 am)
2018 Mac mini - macOS Catalina
Offline
peavine wrote:The script works fine as long as the dictionary exists.
So check if it exists:Applescript:
tell application id "com.apple.systemevents" -- System Events.app
set scriptable to has scripting terminology of (get first application process whose frontmost is true)
end tell
Thanks Shane.
I never saw this feature before.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 8 janvier 2020 10:52:57
Offline
If you wanted to avoid system Events as well, you could use this:
Applescript:
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set activeApp to current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()
set theBundle to current application's NSBundle's bundleWithURL:(activeApp's bundleURL())
return ((theBundle's objectForInfoDictionaryKey:"NSAppleScriptEnabled") is not missing value)
On my Mac, that's about 50 times quicker than System Events, which takes more than 0.1 seconds.
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
If you wanted to avoid system Events as well, you could use this:
Applescript:
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set activeApp to current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()
set theBundle to current application's NSBundle's bundleWithURL:(activeApp's bundleURL())
return ((theBundle's objectForInfoDictionaryKey:"NSAppleScriptEnabled") is not missing value)
On my Mac, that's about 50 times quicker than System Events, which takes more than 0.1 seconds.
Thanks again.
I will save this script so that I don't forget it.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 8 janvier 2020 13:38:23
Offline
While working on this topic, I wrote a simple script which is functionally little different than selecting Open Dictionary from the File menu of Script Editor. The very-slight advantages of this script are 1) it contains only the dictionaries the user wants, and 2) it can be run just about any way a script can be run, which is not the case with my script included above. I tested this with most applications that have a dictionary but there may be a few that won't work.
Applescript:
property defaultItem : missing value
set appList to {"Finder", "Mail", "Preview", "Safari", "Script Editor", "StandardAdditions", "System Events", "TextEdit"}
choose from list appList default items {defaultItem} with prompt "Select a Dictionary:"
if result = false then
error number -128
else
set selectedApp to item 1 of result
end if
if selectedApp = "StandardAdditions" then
set appPath to "Macintosh HD:System:Library:ScriptingAdditions:StandardAdditions.osax:" as alias
else
set appPath to path to application selectedApp
end if
tell application "Script Editor"
if selectedApp is in (name of every window) then
my raiseWindow(selectedApp)
else
activate
open appPath
end if
end tell
set defaultItem to selectedApp
on raiseWindow(selectedApp)
tell application "System Events" to tell process "Script Editor"
perform action "AXRaise" of window selectedApp
end tell
end raiseWindow
Last edited by peavine (2020-01-11 11:56:46 am)
2018 Mac mini - macOS Catalina
Offline
Applescript:
property defaultItem : missing value
set appList to {"Finder", "Mail", "Preview", "Script Editor", "Safari", "System Events", "TextEdit"}
choose from list appList default items {defaultItem} with prompt "Select a Dictionary:"
if result = false then
error number -128
else
set selectedApp to item 1 of result
end if
set appPath to path to application selectedApp
tell application "Script Editor"
activate
open alias appPath
end tell
set defaultItem to selectedApp
The posted script issued :
"Erreur dans Script Editor : Il est impossible d’obtenir alias (alias \"SSD 1000:Applications:Safari.app:\")." number -1728 from alias (alias "SSD 1000:Applications:Safari.app:")
A small edit is required
Applescript:
tell application "Script Editor"
activate
open appPath -- EDITED because appPath is already an alias
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 8 janvier 2020 15:58:59
Offline
Thanks Yvan. I've corrected that.
2018 Mac mini - macOS Catalina
Offline
You may use the simple :
Applescript:
set theApp to choose application as alias
tell application "Script Editor"
activate
open theApp
end tell
At least with 10.13.6, as long as you select an app in the proposed list you will have a scriptable one.
I would be glad to know if other systems behave the same way.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 8 janvier 2020 18:25:15
Offline
You may use the simple :... I would be glad to know if other systems behave the same way.
Yvan. Your script works fine on Catalina and is probably preferred for its simplicity and speed.
Last edited by peavine (2020-01-08 11:55:04 am)
2018 Mac mini - macOS Catalina
Offline
@peavine
Thank you.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 8 janvier 2020 18:48:23
Offline
simplicity and speed.
If you want simplicity and speed, Script Debugger's OpenDictionary command is your answer. Separate lists of running, recent and favorite apps and script libraries, as well as Standard Additions and a general open command. As a bonus, you get a much more detailed presentation of the dictionary.
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
My script works fine and I'll use it a lot, but I did encounter one small issue. To replicate:
1) Copy and paste the script contained below into Script Editor.
2) Run the script, which loads the Script Editor dictionary.
3) Without closing the dictionary, activate Script Editor and run the script again. This brings the Script Editor dictionary to the front but Script Editor continues to show "running" for about 5 seconds.
I wondered if anyone knew how to prevent my script from running as described in 3 above. This does have a practical consequence during normal use in that FastScripts is not responsive during the 5-second period. Thanks.
Applescript:
tell application "System Events"
set appProcess to first application process whose frontmost is true
set appName to name of appProcess
set appDictionary to has scripting terminology of appProcess
end tell
set appFile to path to application appName
if appDictionary then
tell application "Script Editor"
activate
open appFile
end tell
else
display dialog appName & " does not have a dictionary." buttons {"OK"} ¬
cancel button 1 default button 1 with icon stop
end if
Last edited by peavine (2020-01-10 07:30:13 am)
2018 Mac mini - macOS Catalina
Offline
I get the same result on my Mac with Script editor.
Even more interesting is: if I run it with SD with the Debugging option active, I get an error at:
open file of activeApp as alias
This is the error:
Can’t make alias "iMac_HD:Applications:Script Debugger.app:" of application "System Events" into type alias.
This might help you in fix this strange behaviour.
L.
This is the script I used in SD:
Applescript:
tell application "System Events"
set activeApp to first application process whose frontmost is true
set appName to name of activeApp
set appDictionary to has scripting terminology of activeApp
end tell
if appDictionary then
tell application "Script Debugger"
open file of activeApp as alias
activate
end tell
else
display dialog appName & " does not have a dictionary." buttons {"OK"} ¬
cancel button 1 default button 1 with icon stop
end if
This is the corresponding screenshot:
https://funkyimg.com/i/31dfQ.png
Last edited by ldicroce (2020-01-10 12:57:38 am)
Offline
My script works fine and I'll use it a lot, but I did encounter one small issue. To replicate:
1) Copy and paste the script contained below into Script Editor.
2) Run the script, which loads the Script Editor dictionary.
3) Without closing the dictionary, activate Script Editor and run the script again. This brings the Script Editor dictionary to the front but Script Editor continues to show "running" for about 5 seconds.
I wondered if anyone knew how to prevent my script from running as described in 3 above. This does have a practical consequence during normal use in that FastScripts is not responsive during the 5-second period. Thanks.Applescript:
tell application "System Events"
set activeApp to first application process whose frontmost is true
set appName to name of activeApp
set appDictionary to has scripting terminology of activeApp
end tell
if appDictionary then
tell application "Script Editor"
open file of activeApp as alias
activate
end tell
else
display dialog appName & " does not have a dictionary." buttons {"OK"} ¬
cancel button 1 default button 1 with icon stop
end if
Try to use :
Applescript:
tell application "System Events"
set activeApp to first application process whose frontmost is true
set appName to name of activeApp
set appDictionary to has scripting terminology of activeApp
set thePath to path of (file of activeApp)
end tell
if appDictionary then
set maybe to appName & ".sdef"
tell application "System Events" to tell process "Script Editor"
tell menu bar 1 to tell menu bar item 9 to tell menu 1
set menuItems to name of menu items
--> {"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, "get language code.scpt", "Pages.sdef", "Sans titre", "Sans titre 2", "System Events.sdef"}
if menuItems contains maybe then -- if Finder show extensions
click menu item maybe
return -- exit the script
end if
if menuItems contains appName then -- if Finder doesn't show extensions
click menu item appName
return -- exit the script
end if
end tell
end tell
tell application "Script Editor"
open file thePath
activate
end tell
else
display dialog appName & " does not have a dictionary." buttons {"OK"} ¬
cancel button 1 default button 1 with icon stop
end if
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 9 janvier 2020 19:10:39
Last edited by Yvan Koenig (2020-01-12 10:42:59 am)
Offline
Thanks Yvan and ldicroce for the responses. I've revised my script in Post 18 to fix the issue identified by ldicroce. So, all is well.
2018 Mac mini - macOS Catalina
Offline
Would be more logical to put the instruction
Applescript:
set appFile to path to application appName
in the tell application "System Events" block.
But as far as I know, this “enhanced” version doesn't solve your problem #3.
It's what my added instructions were supposed to achieve.
If the frontmost application is Script Editor and if there is a window "Script Editor.sdef" available, it doesn't open the file, it just reveal the window.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 10 janvier 2020 15:00:18
Offline
Would be more logical to put the instruction
Applescript:
set appFile to path to application appName
in the tell application "System Events" block.
But as far as I know, this “enhanced” version doesn't solve your problem #3.
It's what my added instructions were supposed to achieve.
If the frontmost application is Script Editor and if there is a window "Script Editor.sdef" available, it doesn't open the file, it just reveal the window.
Yvan. Thanks for looking at my revised script and for your comments.
As regards the "path to" command line in my script in Post 18, it has always been my understanding that it's best not to put a command in a tell statement if that's not necessary. Because the "path to" command is in Standard Additions, I did not put that instruction in System Events. I'd be interested to learn how this is best handled.
You are correct that the revised version of my script in Post 18 does not fix the error identified as issue #3 in that post. Perhaps I misled, but this revision was only intended to fix the issue identified by ldicroce.
I did try your script which worked well, but it only seemed to fix issue #3 with Script Editor. I tested it with Finder, Safari, and Mail, and the 5-second delay was still present.
I have further refined this issue in the following script. Run it once and there is no delay. Run it again with the Safari dictionary open and there is a 5-second delay, as reflected by the word "running" in the Script Editor Result pane.
Applescript:
set theApp to "Macintosh HD:Applications:Safari.app:" as alias
tell application "Script Editor"
open theApp
end tell
Actually the ideal solution. which entirely avoids this issue, is to get the path to the app's dictionary and to use Finder to open it, as in the following:
Applescript:
set appPath to "Macintosh HD:System:Library:ScriptingAdditions:StandardAdditions.osax:" as alias
tell application "Finder" to open appPath
With other apps, like Safari, this approach just opens the app.
Last edited by peavine (2020-01-10 11:16:35 am)
2018 Mac mini - macOS Catalina
Offline
Running the script :
Applescript:
tell application "System Events"
set activeApp to first application process whose frontmost is true
end tell
returns something :
application process "Script Editor" of application "System Events"
In System Events every disk item has its own path property so it seems logical to ask the app to grab the property.
I enhanced my script.
If I made no error, it would be OK with every scriptable application.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 10 janvier 2020 18:38:23
Offline
Yvan. I tested the following scripts in Script Geek:
Applescript:
tell application "System Events"
set activeApp to first application process whose frontmost is true
set activeName to name of activeApp
set thePath to path of (file of activeApp) as alias
end tell
Applescript:
tell application "System Events"
set activeApp to first application process whose frontmost is true
set activeName to name of activeApp
end tell
set appPath to path to application activeName
With 100 iterations, the first script took 2.5 milliseconds per iteration and the second script 2.3 milliseconds. So, timewise, they are pretty much the same. From a design/style perspective, grouping all the commands under System Events makes sense.
I tried your revised script and encountered a delay as before. I suspect this is something I'm doing wrong.
I rewrote my script to operate in a fashion similar to Nigel's script in the thread linked below. My script displays a dialog with default apps, which are specified in the script, plus apps that are active at the time the script is run. The script no longer has the delay-on-reopen issue.
Thanks again for the help.
https://www.macscripter.net/viewtopic.php?pid=191643
Applescript:
property defaultItem : missing value
set defaultApps to {"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 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 "Dictionaries" 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"
if selectedApp is in (name of every window) then
raiseWindow(selectedApp) of me
else
activate
open appFile
-- set bounds of window 1 to {10, 33, 750, 1065} # option to resize dictionary window
end if
end tell
set defaultItem to selectedApp
on raiseWindow(selectedApp)
tell application "Script Editor" to activate # duplicate activate commands are intentional
tell application "System Events" to tell process "Script Editor" to perform action "AXRaise" of window selectedApp
tell application "Script Editor" to activate
end raiseWindow
Last edited by peavine (2020-01-12 03:54:44 pm)
2018 Mac mini - macOS Catalina
Offline