Is it possible to enhance the Applets with a Preferences menu item or button to open a preferences dialog (maybe built with Dialog Toolkit)?
Should be. You just need a handler that will show the prefs window, then make it the action of a new Preferences
menu item. Something like this:
set appMenu to (current application's NSApp's mainMenu()'s itemAtIndex:0)'s submenu()
set newItem to current application's NSMenuItem's alloc()'s initWithTitle:"Preferences…" action:"showPrefs:" keyEquivalent:""
newItem's setTarget:me
appMenu's insertItem:newItem atIndex:2
...
on showPrefs:sender
...
The tricky thing is to stop it from being added to Script Debugger’s menu when testing. You need to add some test, checking path to me
or the name of current application
, or something.
Thanks, Shane.
I’ll try that. How do I avoid that the Enhanced Applet starts the progress when I add a command to the run handler? The script seems only to wait for dragged items when the run handler is empty.
I fear you’re snookered. I guess you could use an appletFileIsAcceptable
handler to add it after the first file is dropped. You might want to make a feature request.
Uh, I totally forgot about that. Are there any changes in the Enhanced Applet so it’s now possible to add a preferences window to an Droplet?
I don’t believe there were any changes in version 8 in that regard.
Hi Shane. Thanks for your reply. So it would be great if you can transfer this topic to a freature request. Most of my Scripts/Droplets are configurable. As I don’t want the users to modifiy scripts I’m using defaults write/read. But the users often don’t read the docs or forget about some possible settings.
It’s in the database, but I’m not sure exactly what you’re expecting.
Well, I expect an Enhanced Droplet/Applet where I have a Preferences menu item which is also reachable via Cmd+Comma. This could call a subroutine using Dialog Toolkit to provide a preferences window.
The problem is really that the idea of menu-item-driven commands just doesn’t fit in with the AppleScript approach of run and open handlers. Just adding a menu item is what the code earlier in this thread does, and as you’ve seen, it’s not the answer. Honestly, I don’t see any simple solution.
You know, if you could load a dialog toolkits alert/window into an enhanced applets window, the way you can with Display Dialog and Display Alert, that could get you partway there.
Thanks for your replies. The initial tip from Shane is great. I modified it to the following so that it works well with standard Droplets (in the run handler). So I can assign the standard menu item for preferences with a function:
if name of current application is name of me then
set appMenu to (current application's NSApp's mainMenu()'s itemAtIndex:0)'s submenu()
set prefsItem to appMenu's itemAtIndex:2
prefsItem's setAction:"showPrefs:"
prefsItem's setTarget:me
end if
This does not work in the Enhanced Droplet as the run handler breaks the Droplet window. If that could be fixed, it would be great. Or is there another handler to perform initial setup routines? The docs say something about “performSelectorOnMainThread” but how/where do I use it?
Sorry, but I can’t see any obvious solution at this stage.
Okay, good to know that I can stop the investigation now.
So my feature request will be:
- Make the run handler work on Enhanced Droplets so it can be used for initial stuff without forcing the progress window to start / alternatively implement an init handler which will be executed once before the droplet window accepts input
With the current Beta 8.0.3 you have changed the run handler handling for enhanced droplets. So I tried if I can use them now. Everything seems to work but when I use your example in the first answer, the showPrefs: handler is not be able to show a dialog. I know that the handler is called because I tried to insert a beep command but the dialog or alert won’t appear.
can you post a version of the script that illustrates this issue?
Here’s a simplified version. It should open a dialog when I execute the preferences menu command or press ⌘,.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
use framework "AppKit"
on run
set theLastClipboard to ""
if name of current application is name of me then
set appMenu to (current application's NSApp's mainMenu()'s itemAtIndex:0)'s submenu()
set prefsItem to (current application's NSMenuItem's alloc()'s initWithTitle:"Preferences…" action:"showPrefs:" keyEquivalent:",")
prefsItem's setTarget:me
appMenu's insertItem:prefsItem atIndex:2
end if
-- my showPrefs:true
end run
on open droppedItems
-- my showPrefs:true
display alert item 1 of droppedItems as string
end open
on showPrefs:sender
beep 2
display alert "Settings"
end showPrefs:
The scripting additions like “display alert” or “display dialog” do not seem to work in this case.
You can use Shane’s “Dialog Toolkit Plus” script library to display a dialog (works perfect) or you write your own dialog with AppleScriptObjC. For the latter you have to make sure that the call takes place in the correct thread:
on showPrefs:sender
if current application's NSThread's isMainThread() then
my performShowPrefs:(missing value)
else
its performSelectorOnMainThread:"performShowPrefs:" withObject:(missing value) waitUntilDone:true
end if
end showPrefs:
on performShowPrefs:args
-- your dialog code
end performShowPrefs:
Edit: You must also note that the run handler is not executed in some cases. See: Enhanced applet questions - #15 by alldritt
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.