一大波 AppleScript 來襲:開啟 Chrome 視窗、調整 DevTool 位置、載入 Sublime 專案、執行 ITerm 命令 等等

wanyaxing發表於2018-08-10

前言

前陣子說過,發現了 Mac 排程中心的正確用法,在三屏顯示器下很酷,後來又實現了讓 mac 和外聯的顯示器一起隨機成同一個桌布,真是賞心悅目。

想要更高效

然而,如何在桌面空間裡為一堆專案視窗進行自動化佈局這件事一直讓我念念不忘,好在現在有了收穫。

一大波 AppleScript 來襲:開啟 Chrome 視窗、調整 DevTool 位置、載入 Sublime 專案、執行 ITerm 命令 等等

一大波 Applescript 指令碼

此處只分享指令碼和用法,具體組裝需要各人自行領會,菜端上來了,怎麼吃,請自行動筷。

我的工作環境裡,主要涉及 Chrome Sublime iTerm 等軟體,以下程式碼供參考。

開一個新視窗(不是新標籤)

方法:

on newWindow(name, openStr, newWindowStr)
    tell application "Dock"
        activate
    end tell
    tell application "System Events"
        tell process "Dock"
            set frontmost to true
            activate
            tell list 1
                tell UI element name
                    perform action "AXShowMenu"
                    delay 1
                    key code 126 -- up arrow
                    tell menu name
                        try
                            tell menu item openStr
                                perform action "AXPress"
                                -- 第一次開啟程式 需要多等幾秒
                                delay 5
                            end tell
                        on error errMsg
                            tell menu item newWindowStr
                                perform action "AXPress"
                            end tell
                        end try
                    end tell
                end tell
            end tell
        end tell
    end tell
end newWindow
複製程式碼

用法示例:

newWindow("Google Chrome", "開啟", "開啟新的視窗")
複製程式碼

說明:

  • 將常用的軟體放置到 Dock 中,右擊的話可以看到該軟體的選單,選單中的文字,即可作為引數傳入上面給到的方法。
  • 如開啟一個新的 google 瀏覽器視窗,這裡有兩個引數,第一個引數是當前沒有 google 瀏覽器程式存在的命令,第二個引數是如果有 google 瀏覽器程式存在的命令。
  • 其他軟體類似,具體命令你右鍵點一下看文字。

一大波 AppleScript 來襲:開啟 Chrome 視窗、調整 DevTool 位置、載入 Sublime 專案、執行 ITerm 命令 等等

一大波 AppleScript 來襲:開啟 Chrome 視窗、調整 DevTool 位置、載入 Sublime 專案、執行 ITerm 命令 等等

移動視窗到指定位置

方法:


on moveBounds(name, topLeftX, topLeftY, bottomRightX, bottomRightY)
    tell application name
        set bounds of front window to {topLeftX, topLeftY, bottomRightX, bottomRightY}
    end tell
end moveBounds

on sizePosition(name, topLeftX, topLeftY, width, height)
    tell application "System Events" to tell application process name
        tell window 1
            set {position, size} to {{topLeftX, topLeftY}, {width, height}}
        end tell
    end tell
end sizePosition
複製程式碼

用法示例:

sizePosition("Google Chrome", 40, 15, 1828, 1057)
moveBounds("Google Chrome", 1519, 116, 1919, 874)
複製程式碼

說明:

  • 這裡提供了兩個方法,注意引數的區別。
  • 之所以有兩種方法,是因為這兩種方法並非總是生效的,具體軟體具體情況不同,請使用時自行選用。
  • 至於如何取得準確的座標位置,有個簡單方法就是使用各類截圖工具,如snipaste等。

一大波 AppleScript 來襲:開啟 Chrome 視窗、調整 DevTool 位置、載入 Sublime 專案、執行 ITerm 命令 等等

開啟 Chrome 的 DevTool 並切換成獨立視窗然後移動到指定位置

方法:

on getFrontWindow()
    tell application "System Events"
        repeat with theapp in (every application process whose visible is true and frontmost is true)
            repeat with ew in (every window of theapp)
                return ew
            end repeat
        end repeat
    end tell
end getFrontWindow

on openChromeDevTool()
    tell application "Google Chrome"
        activate
    end tell
    tell application "System Events"
        key code 53 --esc
        -- 開啟開發者工具
        delay 1
        key code 34 using {option down, command down}
    end tell
    -- 切換工具視窗
    delay 3
    set frontWindowName to name of getFrontWindow() as string

    if {frontWindowName does not start with "DevTools"} then
        --display dialog frontWindowName
        tell application "System Events"
            key code 2 using {shift down, command down}
            delay 2
        end tell
    end if
    -- 移動開發者工具
    moveBounds("Google Chrome", 1920, -36, 3410, 1043)
end openChromeDevTool
複製程式碼

用法示例:

openChromeDevTool()
複製程式碼

說明:

  • 這是我個人的一個特殊需求
  • 在開發移動端網頁時,我常常會獨立 DevTool 的視窗,
  • 所以我用上述程式碼實現了視窗獨立並移動到副屏顯示器的需求。

用 Sublime 載入一個專案

方法:

on sublOpenWorkspace(name)
    do shell script "\"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl\" -n  --project \"/Users/AsinW/Library/Application Support/Sublime Text 3/Packages/User/Projects/" & name & ".sublime-workspace\""
end sublOpenWorkspace
複製程式碼

用法示例:

    delay 1
    newWindow("Sublime Text", "開啟", "New Window")
    delay 1
    sizePosition("Sublime Text", 0, 15, 1514, 1080)
    delay 1
    sublOpenWorkspace("jiyingshou_api3")
    delay 3
    sublOpenWorkspace("jiyingshou_admin")
複製程式碼

說明:

  • 該示例展示了開啟一個新的 Sublime 視窗,移動到指定位置,然後載入多個專案的流程。
  • 注意:在上文的方法裡,使用了 Sublime Text.app 的絕對路徑,請大家自行更改為自己電腦中的對應路徑。
  • 至於如何管理 Sublime 的專案檔案,如何生成 sublime-workspace 這樣的檔案,請大家自行探索(這是另一件事了,按理說大家應該已經很早就接觸到了,所以此處不作長篇大論)。

一大波 AppleScript 來襲:開啟 Chrome 視窗、調整 DevTool 位置、載入 Sublime 專案、執行 ITerm 命令 等等

開啟 iTerm2 並執行命令

方法:


-- 在當前皮膚執行命令
on iTermCmdCurrentTab(cmdStrs)
    tell application "iTerm"
        activate
        tell current window
            tell current session
                repeat with cmdStr in cmdStrs
                    write text cmdStr
                end repeat
            end tell
        end tell
    end tell
end iTermCmdCurrentTab

-- 開啟一個新標籤,並執行命令
on iTermCmdNewTab(cmdStrs)
    tell application "iTerm"
        activate
        tell current window
            create tab with default profile
            tell current session
                repeat with cmdStr in cmdStrs
                    write text cmdStr
                end repeat
            end tell
        end tell
    end tell
end iTermCmdNewTab


-- 在當前介面橫向再切一個皮膚出來並執行命令
on iTermCmdInPane(cmdStrs)
    tell application "iTerm"
        activate
        tell current window
            tell current tab
                tell current session
                    split horizontally with same profile
                end tell
                repeat with ss in sessions
                    select ss
                end repeat
                tell current session
                    repeat with cmdStr in cmdStrs
                        write text cmdStr
                    end repeat
                end tell
            end tell
        end tell
    end tell
end iTermCmdInPane

-- 指定皮膚高度
on iTermSetRows(num)
    tell application "iTerm"
        tell current session of current window
            set rows to num
        end tell
    end tell
end iTermSetRows

-- 啟用指定皮膚
on iTermActiveSession(num)
    tell application "iTerm"
        tell current tab of current window
            select item num in sessions
        end tell
    end tell
end iTermActiveSession
複製程式碼

用法示例:

        delay 1
        newWindow("iTerm", "開啟", "New Window (Default Profile)")

        delay 1
        iTermCmdCurrentTab({"cd /Users/AsinW/svnBox/jiyingshou/web/pc"})
        iTermCmdInPane({"cd /Users/AsinW/svnBox/jiyingshou/web/pc", "npm run dev"})
        iTermSetRows(3)
        iTermActiveSession(0)

        delay 1
        iTermCmdNewTab({"cd /Users/AsinW/svnBox/jiyingshou/web/mobile4"})
        iTermCmdInPane({"cd /Users/AsinW/svnBox/jiyingshou/web/mobile4", "npm run dev"})
        iTermSetRows(3)
        iTermActiveSession(0)

        delay 1
        moveBounds("iTerm", -1187, 136, -1, 935)
複製程式碼

說明:

  • 上述示例描述了開啟 ITerm ,並在兩個標籤四個皮膚中執行對應命令的流程。
  • 在命令執行完最後,該視窗被移到了左側顯示器裡了哦。

一大波 AppleScript 來襲:開啟 Chrome 視窗、調整 DevTool 位置、載入 Sublime 專案、執行 ITerm 命令 等等

後語

工具是死的,人是活的。

有很多大家耳熟能詳的工具或方法,只是聽過,沒有用過,因為你沒有想象到自己使用的場景,等到頓悟的那一天,會發現,哦,可以這樣那樣的用。

以此記錄,供大家參考或收藏或路過。

原文來自阿星的部落格:wanyaxing.com/blog/201808…

相關文章