hey! this is something that i was struggling with by my self trying everything i could think of, and searching the internet but i just dont really understand applescripting enough yet… this is the first thing i have ever tried doing with it. basically what i want to do is, currently no matter what applicaition is running and is the current app in the menu bar, you can press “command, option, space” and the spotlight search window will pop up. what i want to do is to write a script so that i can have it in my dock, and give it a nice spotlight icon. essentialy just for show, and just to kinda get my feet wet so to speak with applescripting. so i can click on it and it will lauch the spotlight search window. essesntially just issuing the keystroke command to the system. this is what i was thinking might do it but it is not working, i would really appreciate any ideas any of you may have
I know you can change the keyboard shortcuts to open the spotlight menu in the finder.
so you could change it to any keystroke you want i guess.
but anyhow this opens the menu in the top right hand corner.
tell application "System Events"
tell process "Finder"
activate
set frontmost to true
keystroke " " using {command down}
end tell
end tell
and this opens the spotlight window
tell application "System Events"
tell process "Finder"
activate
set frontmost to true
keystroke " " using {option down,command down}
end tell
end tell
incidently you may want to save your script as an application bundle so you can create an “.icns” icon of the spotlight logo and drop it
into the resources folder. so your icon sticks around abit longer… copying and pasting in the get info window is only good for so much.
Sweet! that worked perfectly! it really does not look to complicated, i guess i just really need to learn how the language flows, it looks like i maybe was not to far off, but obivously had a very long way to go. but any way, thanks for the prefect and super fast response!
Nice tip, but if the user changes the shortcut key and we can’t change the script. Is my first time I take a look into applescript, after a lot of searching, test&error and reading I started to play with the UIElementInspector and I saw an extrange thing:
Spotlight icon:
<AXApplication: “SystemUIServer”>
Other menu icon:
<AXApplication: “SystemUIServer”>
I miss an AXMeenuBar level, tried to find the spotlight browsing by children from the application level and it doesn’t appear the tree is something like this:
AXApplication
AXChildren
AXMenuBar
AXChildren
AXMenuBarItem
AXMenuBar
AXChildren
AXMenuBarItem
Where the AXMenuBarItem are all the icons of the menu (wifi, screen, sound, battery, time, user, bluetooth, …) except the spotlight one
How can we access this icon without using the default keyboard shortcut? Is this a bug of Tiger or a feature? The spotlight windows can be opened in other way?
There is a (weird) way.
The script reads the keyboard shortcut from ~/Library/Preferences/com.apple.universalaccess.plist (#30 of UserAssignableHotKeys with sybmolichotkey = 64).
The coding of the modifier keys are the bits 17 - 20 (17=shift, 18=control, 19=option, 20=command) from the longword modifier, the key is not coded.
set pList to (path to preferences folder as string) & "com.apple.universalaccess.plist"
tell application "System Events"
set pList to property list file pList
set pListItems to property list items of property list item "UserAssignableHotKeys" of contents of pList
set theItem to 1st item of pListItems whose value of property list item 4 is 64 -- index of spotlight shortcut
set {theKey, keyEnabled, ModifierKeys, idx} to value of property list items of theItem
if keyEnabled = 1 then
set ModifierKeys to ModifierKeys / 131072 as integer -- strip bits 0 - 16
if ModifierKeys div 8 mod 2 = 1 then key down command
if ModifierKeys div 4 mod 2 = 1 then key down option
if ModifierKeys div 2 mod 2 = 1 then key down control
if ModifierKeys mod 2 = 1 then key down shift
key code theKey
key up {command, option, control, shift}
end if
end tell
looking into my preferences folder I found other file com.apple.spotlight.plist wich has this properties:
HotKeys
MenuVirtualKey → 49
MenuVirtualKeyEnabled → Yes
MenuVirtualKeyModifier → 262144
WindowVirtualKey → 49
WindowVirtualKeyEnabled → yes
WindowVirtualKeyModifier → 786432
I I compare the Modifier of universalaccess (1048576 and 1572864) with the spotlight plist files, they are different. I think that maybe the best way of check te shortcut keys of spotlight? why is this different?
This is extremely slick, Stefan, but has a wierd side effect on my machine: it makes my Script Debugger 4.app window transparent and dims it substantially. Everything still works, but the only way to recover is to close the window and open it again. This effect is not seen in the Script Editor.app, so clearly something was trapped by SD4. I’ll ask in their list to see what gives.
Odder and odder, Stefan. When I changed my hot keys for Spotlight (swapping them with Quicksilver’s, i.e. Spotlight’s set to the default command-space and command-option space, and Quicksilver set back to its default: control-space) then the script doesn’t work. I assume that when there is no “special” hot key, “UserAssignableHotKeys” goes away.
This fixes that:
set pList to (path to preferences folder as string) & "com.apple.universalaccess.plist"
tell application "System Events"
set pList to property list file pList
set pListItems to property list items of property list item "UserAssignableHotKeys" of contents of pList
set theItem to 1st item of pListItems whose value of property list item 4 is 64 -- index of spotlight shortcut
set {theKey, keyEnabled, ModifierKeys, idx} to value of property list items of theItem
if keyEnabled = 1 then
set ModifierKeys to ModifierKeys / 131072 as integer -- strip bits 0 - 16
if ModifierKeys div 8 mod 2 = 1 then key down command
if ModifierKeys div 4 mod 2 = 1 then key down option
if ModifierKeys div 2 mod 2 = 1 then key down control
if ModifierKeys mod 2 = 1 then key down shift
key code theKey
key up {command, option, control, shift}
else
keystroke space using {command down}
end if
end tell