我想用URL 方案打开Mac App Store。
在10 月之前,我使用了下面的链接
macappstore://ax.search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=
后跟一个搜索词,如things
.
我想用URL 方案打开Mac App Store。
在10 月之前,我使用了下面的链接
macappstore://ax.search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=
后跟一个搜索词,如things
.
看起来 URL 在 10.11 中发生了非常细微的变化。新网址如下:
macappstores://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?mt=12&ign-mscache=1&q=...
以下基于 AppleScript 的解决方案只是权宜之计,但在某些情况下可能就足够了;例如,我想从支持 AppleScripts的Alfred 2执行基于命令行的 App Store 搜索。
macappstore://
方法,而在 10.9+ 上,它使用GUI 脚本——通过可访问性 API,而不是通过发送击键,所以它应该相当健壮。要试用该脚本,请将其粘贴到 AppleScript 编辑器中,在顶部放置一个示例调用 - 例如my searchAppStore("dash")
,然后运行它。
# Performs an App Store search via the App Store.app.
# Example:
# my searchAppStore("dash")
# Works on OS X 10.8+:
# - 10.8: Uses the macappstore:// URL scheme.
# - 10.9+: Sadly, GUI scripting (i.e., simulated user input) must be used,
# which requires that access for assistive devices be enabled as *one-time setup*
# for the application in whose context this script runs.
# While this script attempts to facilitate enabling this feature,
# user intervention is - by OS design - required.
on searchAppStore(searchTerm)
if my isPreMavericks() then # assumes: OS X 10.8
# We can use the macappstore:// URL scheme to submit a search.
tell application "System Events" to open location "macappstore://ax.search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=" & searchTerm
return
else # OS X 10.9+: alas, we must use GUI scripting.
# First, ensure that access for assistive devices is enabled -
# otherwise, GUI scripting won't work.
my ensureAssistiveAccess()
# Activate (and launch, if necessary) the App Store app.
tell application "App Store" to activate
# Use GUI scripting to simulate an interactive search.
tell application "System Events"
tell application process "App Store"
# !! CAVEAT: The spelling of the toolbar UI element changed from 'tool bar' (10.8)
# !! to 'toolbar' (10.9) - if you compile this script on 10.9, you'll end
# !! up with 'toolbar', which will break when PASTED into a 10.8 AppleScript
# !! window. If you're on 10.8 and get an error, change 'toolbar' to 'tool bar'.
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 # Timeout is iteration count * delay period below.
set value of attribute "AXValue" of searchTextField to searchTerm
# !! Sadly, when App Store.app was just launched by this script, attempts to
# !! assign to the search field initially fail silently, until some time
# !! after startup. We simply keep trying for a while until we succeed.
if (value of attribute "AXValue" of searchTextField) = searchTerm then
# Click button to submit search.
# [If you run this on 10.8]
# !! On OS X 10.8, if this script just launched App Store.app,
# !! `tell application "App Store" to activate` didn't actually
# !! *activate* the app (it just launched it).
# !! Hence, we simply activate again here - BEFORE we
# !! submit the search - otherwise, it may not work.
tell application "App Store" to activate
click searchSubmitButton
# We're done.
return
end if
delay 0.3
end repeat
# Getting here means that submitting did not succeed within the timeout period.
# Raise an error.
error "Failed to submit search search term to the App Store application."
end tell
end tell
end if
end searchAppStore
# Tries to ensure that access for assistive devices is turned on so as to enable GUI scripting.
# - Up to 10.8.x, access can be turned on programmatically, on demand - via an admin authorization prompt.
# - From 10.9, the best we can do is display a GUI prompt, then open System Preferences to the relevant pane, then exit, telling the user to try again after interactively enabling access.
# Returns:
# Only returns if access is already enabled; throws an error otherwise.
# Example:
# my ensureAssistiveAccess() # throws error, if not enabled and couldn't be enabled programmatically (10.9+)
# # Alternatively, catch the error:
# try
# my ensureAssistiveAccess()
# on error
# # Exit quietly, relying on the prompt to have provided sufficient information.
# return
# end try
on ensureAssistiveAccess()
local ok, isPreMavericks, verOs, verMajor, verMinor, btn
# Determine if access is currently enabled.
tell application "System Events" to set ok to UI elements enabled
if not ok then
# See if we're running 10.8 or below
set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's 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's text item delimiters to orgTIDs
set isPreMavericks to verMajor ≤ 10 and verMinor < 9
if isPreMavericks then # 10.8-: we can try to turn it on ourselves, which will prompt for authorization
try
# Try to turn it on - will prompt for authorization via admin credentials.
tell application "System Events"
set UI elements enabled to true
set ok to UI elements enabled # Check if the user actually provided the authorization.
end tell
end try
else # 10.9+: we cannot turn it on ourselves, it has to be enabled *interactively*, *per application*.
# Try a dummy GUI scripting operation - which we know will fail - in the hope that this will
# get the app at hand registered in System Preferences > Security & Privacy > Privacy > Accessibility.
# ?? Does this work?
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" # ?? how can we deal with other apps that invoke `osascript`, such as Alfred?
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
# We only get here if the user didn't cancel.
# Open System Preferences and show the appropriate pane. (This is the best we can do in guiding the user - further guidance would require the very kind of assistive access we're trying to turn on.)
tell application "System Preferences"
activate
tell pane id "com.apple.preference.security"
reveal anchor "Privacy_Assistive"
end tell
end tell
end try
# We must return false, as we can't easily and reliably wait for the user to finish the operation.
end if
end if
if not ok then
if isPreMavericks then # This indicates that the authorization prompt was aborted; for 10.9+, errMsg was set above.
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
# Indicates if the OS version predates Mavericks (10.9).
# Example: my isPreMavericks() # -> true on 10.8.x and below
on isPreMavericks()
local verOs, verMajor, verMinor
set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's 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's text item delimiters to orgTIDs
return verMajor ≤ 10 and verMinor < 9
end isPreMavericks
只需将“&mt=12”添加到现有的 iTunes Store 链接即可。这会将查询重定向到 Mac AppStore 应用程序而不是 iTunes。
MAS 中的搜索查询示例:
https://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=equinux&mt=12