[英]MacOS Terminal: Is it possible to create a "drag-and-drop" action via script?
有很多帖子解釋了如何將東西拖放到打開的終端 window 中。但我想做的是告訴終端將先前選擇的目錄拖放到另一個應用程序,如 VSCode 或 Renamer。 我還沒有找到任何文件。 有可能嗎? 如果是這樣,有人可以指點我一份文件嗎?
更新:我想用我打算做的事情來澄清我的問題:
先決條件:
“Automator”(MacOS 應用程序)操作應模仿以下步驟:
Automator 動作的“拖放”部分為我提供了一個謎語。
“拖放”操作是手動操作。 在 AppleScript 中,打開文件或文件夾的命令被提供給目標應用程序本身。
第二件事要記住。 使用do script "pwd"命令可以輕松獲取終端的當前工作目錄。 但是do script終端命令返回給腳本的結果始終是 window 選項卡,而不是pwd shell 命令的結果。 一種解決方案是將 pwd 的結果重定向(記住)到一個臨時文本文件中。
set tempFolder to (path to temporary items folder from user domain)
set temp to POSIX path of tempFolder & "workingDirectory.txt"
tell application "Terminal" to do script ("pwd > " & temp) in selected tab of window 1
set curDirPosixPath to paragraph 1 of (read file ((tempFolder as text) & "workingDirectory.txt"))
set curDirHFSPath to curDirPosixPath as POSIX file as Unicode text
tell application "Visual Studio Code" to open curDirHFSPath
. 注意:其他可能的解決方案(我不喜歡)是在執行 pwd 命令后解析終端 window 的文本內容。 您可以使用屬性內容(前窗口的選定選項卡)獲取內容。
打開 Automator,選擇創建新文檔。 選擇創建新的快速操作(服務)。 設置工作流接收任何應用程序中的當前文件夾。 從庫中添加運行 AppleScript 操作。 編輯內容:
on run {input, parameters}
set curDirHFSPath to (item 1 of input) as text
tell application "Visual Studio Code" to open curDirHFSPath
end run
將此快速操作另存為服務。 現在,當右鍵單擊該文件夾時,會打開上下文菜單,您可以在其中選擇並運行此服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.