AppleScript - 如何搜尋字串的結果?

AppleScript - 如何搜尋字串的結果?

我正在嘗試搜尋 AppleScript 的結果以確定是否出現字串。

運行這段程式碼:

tell application "System Events" to tell process "Box Sync" to ¬
    tell menu bar item 1 of menu bar 2
        click
        get menu items of menu 1
        set myStatus to menu items of menu 1
        set myResult to result
        return myResult             
    end tell

結果是:

{menu item "Files Synced" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 2 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Pause" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 4 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Open Box Sync Folder" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Open Box.com" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 7 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Preferences…" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 9 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Quit" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events"}

現在我想搜尋此結果以查看是否存在“文件已同步”。然而運行

 myResult contains "Files Synced"

再次給我列印整個結果。如何搜尋此結果以確定字串是否存在?

答案1

return myResult您兩次都得到相同的列印輸出,因為您在第一次運行後 沒有刪除該行。return當腳本到達此命令時,將始終終止它。

▸ 另外,請更改此:

    set myStatus to menu items of menu 1

對此:

    set myResult to name of menu items of menu 1

▸ 刪除這一行:

    get menu items of menu 1

和這一行:

    set myResult to result

(他們實際上什麼也沒做。)

您的最終腳本將如下所示:

    tell application "System Events" to tell process "Box Sync" to ¬
        tell menu bar item 1 of menu bar 2
            click
            set myResult to name of menu items of menu 1
            myResult contains "Files Synced"
        end tell

這將返回truefalse

或者,沒有明確變數宣告(並使用 AppleScript 預先定義result變數):

    tell application "System Events" to tell process "Box Sync" to ¬
        tell menu bar item 1 of menu bar 2
            click
            get the name of menu items of menu 1
            result contains "Files Synced"
        end tell

如果您需要任何說明或有任何進一步疑問,請隨時發表評論,我會盡快回覆您。如果它有助於解決您的問題,請考慮選擇它作為您接受的答案。

相關內容