如何在 AppleScript 中連接字串

我習慣在 JavaScript、Swift、Python 和其他語言中使用 + 運算子來連接字串。 所以我試著在 AppleScript 中也使用這個運算子,但是它沒有起作用。 在 AppleScript 中,你需要使用 & 運算子: set example to "hello" "testing " & example ☝️ 這是 macOS 內建的 Script Editor(腳本編輯器)應用程式,我發現這是與 AppleScript 進行實驗的最佳方法。

如何在 macOS Finder 中添加「在終端機中打開」圖示

假設我在 Finder 中打開了一個資料夾,而我想要在終端機中打開它。 為了讓這個操作更加方便,我決定在 Finder 中添加一個「在終端機中打開」圖示: 下面是如何實現這一點的方法。 首先打開 Automator,選擇「應用程式」。 在操作列表中搜索「執行 AppleScript」,然後粘貼以下內容: on run {input, parameters} tell application "Finder" set myPath to (POSIX path of (target of front window as alias)) end tell tell application "Terminal" do script "cd " & myPath activate end tell return input end run 如果你更喜歡使用「執行 Shell 腳本」,可以使用以下 Bash 腳本: osascript -e ' tell application "Finder" set myPath to (POSIX path of (target of front window as alias)) end tell tell application "Terminal" do script "cd " & myPath activate end tell ' 第一個腳本運行的是 osascript(這是一個運行 AppleScript 的腳本),內容如下:...

從 macOS 選單列執行 Node.js 腳本

我想要快速添加從選單列執行 Node.js 腳本的方法。 有許多選擇,包括製作自己的 macOS 選單列應用程式或使用第三方應用程式。 但是 macOS 也提供了一種簡單的方式,使用古老的 AppleScript。 在 macOS 上打開 Script Editor 應用程式。 開啟它的設定並啟用 在選單列顯示 Script 選單 : 現在點擊檔案 → 新增選單以開啟腳本編輯器: 為了執行我的 Node.js 腳本,我添加了這行: do shell script "cd /Users/myself/dev; /opt/homebrew/bin/node script.js" 作為一名 JavaScript 開發者,我必須提醒您也可以使用JavaScript for Automation (JXA) 來達到相同的效果: const app = Application.currentApplication() app.includeStandardAdditions = true app.doShellScript('cd /Users/myself/dev; /opt/homebrew/bin/node script.js') 但在這種情況下,我更喜歡使用 AppleScript,這更符合「原生」的感覺。 我使用了 cd 以便 Node 可以讀取我放在該資料夾的 .env 檔案。 然後我點擊腳本名稱,添加一個好的名字並保存到我用戶的腳本資料夾中,該資料夾位於 ~/Library/Scripts。 完成所有這些步驟後,我就可以在選單列上找到我的腳本,像這樣: 就是這樣! 現在我可以執行它,當腳本執行完成時,一個旋轉的齒輪會出現,通知我: 非常酷!