Next, the AppleScript solution is just a stop, but may be enough in certain situations; for example, I want to search for command line App Store applications from Alfred 2 , which supports AppleScripts.
- script OSX 10.8 + ( 10.10, ).
- 10.8
macappstore://, 10.9+ GUI- - API , , . - , 10.9+ , script, - - ( script),.
script, AppleScript, - . my searchAppStore("dash"), .
on searchAppStore(searchTerm)
if my isPreMavericks() then
tell application "System Events" to open location "macappstore://ax.search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=" & searchTerm
return
else
my ensureAssistiveAccess()
tell application "App Store" to activate
tell application "System Events"
tell application process "App Store"
set searchTextField to get text field 1 of group 7 of tool bar 1 of front window
set searchSubmitButton to get button 1 of searchTextField
set ok to false
repeat with i from 1 to 20
set value of attribute "AXValue" of searchTextField to searchTerm
if (value of attribute "AXValue" of searchTextField) = searchTerm then
tell application "App Store" to activate
click searchSubmitButton
return
end if
delay 0.3
end repeat
error "Failed to submit search search term to the App Store application."
end tell
end tell
end if
end searchAppStore
on ensureAssistiveAccess()
local ok, isPreMavericks, verOs, verMajor, verMinor, btn
tell application "System Events" to set ok to UI elements enabled
if not ok then
set {orgTIDs, AppleScript text item delimiters} to {AppleScript text item delimiters, {"."}}
set verOs to system version of (system info)
set verMajor to first text item of verOs as number
set verMinor to second text item of verOs as number
set AppleScript text item delimiters to orgTIDs
set isPreMavericks to verMajor โค 10 and verMinor < 9
if isPreMavericks then
try
tell application "System Events"
set UI elements enabled to true
set ok to UI elements enabled
end tell
end try
else
try
tell application "System Events" to windows of process "SystemUIServer"
end try
set appName to name of current application
if appName = "osascript" then set appName to "Terminal"
set errMsg to "You must turn on ACCESS FOR ASSISTIVE DEVICES for application '" & appName & "' (System Preferences > Security & Privacy > Privacy > Accessibility) first, then retry."
try
display dialog errMsg & linefeed & linefeed & "Press OK to open System Preferences now; unlock, if necessary, then locate the application in the list and check it." with icon caution
tell application "System Preferences"
activate
tell pane id "com.apple.preference.security"
reveal anchor "Privacy_Assistive"
end tell
end tell
end try
end if
end if
if not ok then
if isPreMavericks then
set errMsg to "You must turn on ACCESS FOR ASSISTIVE DEVICES first, via System Preferences > Accessibility > Enable access for assistive devices"
end if
error errMsg
else
return true
end if
end ensureAssistiveAccess
on isPreMavericks()
local verOs, verMajor, verMinor
set {orgTIDs, AppleScript text item delimiters} to {AppleScript text item delimiters, {"."}}
set verOs to system version of (system info)
set verMajor to first text item of verOs as number
set verMinor to second text item of verOs as number
set AppleScript text item delimiters to orgTIDs
return verMajor โค 10 and verMinor < 9
end isPreMavericks