URL方案Mac App Store搜索

时间:2013-10-26 13:57:00

标签: app-store osx-mavericks osx-yosemite url-scheme

我想使用网址打开 Mac App Store

october 之前我使用了下面的链接

macappstore://ax.search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=

后跟搜索字词things

有关OS X 10.9 Mavericks上新的 URL方案的概念吗?

3 个答案:

答案 0 :(得分:4)

以下基于AppleScript的解决方案只是一个权宜之计,但在某些情况下可能就足够了;例如,我想从支持AppleScripts的Alfred 2执行基于命令行的App Store搜索。

  • 脚本适用于OSX 10.8 + (测试到10.10,这是撰写本文时的最新版本)。
  • 在10.8上,它使用macappstore://方法,而在10.9+上使用 GUI脚本 - 通过可访问性API,而不是通过发送击键,因此它应该相当健壮。< / LI>
  • 因此,在10.9+上,辅助设备的访问必须最初专门为执行脚本的应用程序启用,这需要管理权限,并且 - 通过OS设计 - 几个手动步骤(脚本尝试为了方便)。
  • 要尝试脚本,请将其粘贴到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
    

答案 1 :(得分:4)

只需添加&#34;&amp; mt = 12&#34;到现有的iTunes Store链接。 这会将查询重定向到Mac AppStore App而不是itunes。

MAS中搜索查询的示例:

https://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?q=equinux&mt=12

答案 2 :(得分:4)

看起来网址在10.11中略有变化。新URL如下:

macappstores://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?mt=12&ign-mscache=1&q=...