applescript & Automator配置safari simulator快捷鍵

如果你已經(jīng)厭煩用鼠標(biāo)操作來(lái)打開(kāi)safari中的“開(kāi)發(fā)->simulator->Url”來(lái)打開(kāi)調(diào)試工具的話(huà),這篇文章能幫助到你

本文中的腳本可以在github中查看
https://github.com/realjade/open-safari-simulator-applescript

配置Automator服務(wù)

1)在Launchpad中找到Automator,應(yīng)該在《其他》里面,然后打開(kāi)

Automator圖標(biāo).png

2)在選取文稿類(lèi)型的彈框中選擇“服務(wù)”,然后點(diǎn)擊選取,如下圖:

選取文稿類(lèi)型彈框.png

3)在“服務(wù)”收到的選擇框中選擇“沒(méi)有輸入”

Paste_Image.png

4)在“實(shí)用工具 -> 《雙擊》運(yùn)行AppleScript ”,會(huì)出現(xiàn)右邊的“運(yùn)行AppleScript”腳本編寫(xiě)框

Paste_Image.png

5)把下面的腳本全部替換到框中即可
本文中的腳本可以在github中查看
https://github.com/realjade/open-safari-simulator-applescript

set urls to {}
tell application "System Events"
    tell process "Safari"
        set developMenu to menu 1 of menu item "Simulator" of menu 8 of menu bar 1
        set allUIElements to entire contents of developMenu
        
        repeat with anElement in allUIElements
            try
                set tmpName to name of anElement
                log "sims: " & tmpName
                set boolName to (offset of "." in tmpName) is not 0
                
                if boolName is true then
                    set urls to urls & tmpName
                end if
            end try
        end repeat
        log urls
    end tell
end tell
set urlLen to count of urls

if urlLen is 0 then
    display dialog "沒(méi)有在模擬器中找到能debug的url"
else if (urlLen is 1) then
    tell application "Safari"
        activate
        tell application "System Events"
            tell process "Safari"
                click menu item (item 1 of urls) of menu 1 of menu item "Simulator" of menu 8 of menu bar 1
            end tell
        end tell
    end tell
else
    tell application "System Events"
        activate
        set chooseUrl to choose from list urls with title "請(qǐng)選擇您的URL" default items (item 1 of urls)
    end tell
    if chooseUrl is not false then
        tell application "Safari"
            activate
            tell application "System Events"
                tell process "Safari"
                    click menu item (item 1 of chooseUrl) of menu 1 of menu item "Simulator" of menu 8 of menu bar 1
                end tell
            end tell
        end tell
    end if
end if
Paste_Image.png

保存完后,自己點(diǎn)擊運(yùn)行按鈕,看下是否可用。

如果腳本有更新,則在A(yíng)utomator里面,“文件->打開(kāi)最近使用”選擇自己的創(chuàng)建的服務(wù),然后更新下腳本即可

Paste_Image.png

6)保存,command+s。輸入服務(wù)名稱(chēng):safari-simulator-debug。名字隨意自己起就好了

Paste_Image.png

以上applescript & Automator配置完成

配置快捷鍵

1)打開(kāi)“系統(tǒng)偏好設(shè)置 -> 鍵盤(pán) -> 快捷鍵 -> 服務(wù)”

Paste_Image.png

2)在右邊的列表中找到“通用 -> safari-simulator-debug”,然后點(diǎn)擊右邊的“添加快捷鍵”,設(shè)置成command+alt+i,和chrome打開(kāi)開(kāi)發(fā)者工具的快捷鍵一樣

Paste_Image.png

3)打開(kāi)xcode模擬器,然后打開(kāi)一個(gè)webview的頁(yè)面,按下快捷鍵command+alt+i,注意:讓xcode的simulator處于活動(dòng)狀態(tài)再按快捷鍵,就會(huì)執(zhí)行我們剛才的applescript腳本。如果只有一個(gè)url則會(huì)自動(dòng)打開(kāi)該URL,如果多于1個(gè)則會(huì)讓你進(jìn)行選擇需要打開(kāi)的url,選擇一個(gè)URL即可。如下圖:

多個(gè)URL情況.png

以上配置完成。

Q&A

1)Q:


運(yùn)行錯(cuò)誤

A:
主要是因?yàn)椤癤code Simulator”沒(méi)有權(quán)限,需要把權(quán)限添加進(jìn)去
權(quán)限加好以后,還會(huì)出現(xiàn)這種錯(cuò)誤,應(yīng)該是safari沒(méi)啟動(dòng)起來(lái),點(diǎn)擊“OK”后,再執(zhí)行一遍
打開(kāi)“系統(tǒng)偏好設(shè)置->安全性與隱私->隱私->輔助功能”加上“Xcode Simulator”,

Paste_Image.png
  • 由于點(diǎn)擊“+”的時(shí)候選擇Simulator時(shí)找不到的話(huà)可以這樣添加:
    1)在圖標(biāo)出右鍵,然后“選項(xiàng)->在Finder中顯示”


    Paste_Image.png

    2)把打開(kāi)的圖標(biāo)拖到框里面即可添加成功。

Paste_Image.png

其他選項(xiàng)加上也可以:

Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png

接下來(lái)繼續(xù)研究怎么一鍵配置,敬請(qǐng)期待...
alfred workflow
已經(jīng)上傳到github倉(cāng)庫(kù)了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容