简体   繁体   English

MacOS 终端:是否可以通过脚本创建“拖放”操作?

[英]MacOS Terminal: Is it possible to create a "drag-and-drop" action via script?

There are many posts that explain how to drag-and-drop things into an open Terminal window. But what I would like to do is to tell Terminal to drag-and-drop a previously selected directory onto another application like VSCode or Renamer.有很多帖子解释了如何将东西拖放到打开的终端 window 中。但我想做的是告诉终端将先前选择的目录拖放到另一个应用程序,如 VSCode 或 Renamer。 I have not found any documentation for that.我还没有找到任何文件。 Is it at all possible?有可能吗? And if so, would somebody please point me to a documentation?如果是这样,有人可以指点我一份文件吗?

UPDATE: I'd like to clarify my question with what I intend to do:更新:我想用我打算做的事情来澄清我的问题:

Pre requisites:先决条件:

  • a "work folder" contains folders and files that shall be renamed “工作文件夹”包含应重命名的文件夹和文件
  • the renaming is done by an application called "A better finder renamer" (which allows presets)重命名是由一个名为“A better finder renamer”(允许预设)的应用程序完成的

An "Automator" (MacOS app) action shall imitate these steps: “Automator”(MacOS 应用程序)操作应模仿以下步骤:

  • the "work folder" is right clicked右键单击“工作文件夹”
  • the folder is drag-and-dropped onto the ABFR, which initiates the currently active preset文件夹被拖放到 ABFR 上,这会启动当前活动的预设
  • other actions via bash (like 'mv./ / .*./')...通过 bash 进行的其他操作(如“mv./ / .*./”)...

It is the "drag-and-drop" part of the Automator action that presents a riddle for me. Automator 动作的“拖放”部分为我提供了一个谜语。

The "drag-and-drop" operation is manual operation. “拖放”操作是手动操作。 In AppleScript, instead the command to open the file or folder is given to the destination application itself.在 AppleScript 中,打开文件或文件夹的命令被提供给目标应用程序本身。

Second thing to keep in mind.第二件事要记住。 Getting Terminal's current working directory is easy with its do script "pwd" command.使用do script "pwd"命令可以轻松获取终端的当前工作目录。 But the result of the do script Terminal command returned to the script is always the window tab, not the result of the pwd shell command.但是do script终端命令返回给脚本的结果始终是 window 选项卡,而不是pwd shell 命令的结果。 One solution is to redirect (remember) the result of pwd in a temporary text file.一种解决方案是将 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

. . NOTE: other possible solution (I don't like) is parsing the text contents of Terminal window after pwd command execution.注意:其他可能的解决方案(我不喜欢)是在执行 pwd 命令后解析终端 window 的文本内容。 You can get contents using property contents (of selected tab of front window).您可以使用属性内容(前窗口的选定选项卡)获取内容。

Open Automator, choose create New Document.打开 Automator,选择创建新文档。 Choose create new Quick Action (service).选择创建新的快速操作(服务)。 Set workflow receives current folders in any application .设置工作流接收任何应用程序中的当前文件夹 From library Add Run AppleScript action.从库中添加运行 AppleScript 操作。 Edit it contents:编辑内容:

on run {input, parameters}
    set curDirHFSPath to (item 1 of input) as text
    tell application "Visual Studio Code" to open curDirHFSPath
end run

Save this Quick Action as service .将此快速操作另存为服务 Now when right-clicking the folder, opens the context menu, where you can choose and run this service.现在,当右键单击该文件夹时,会打开上下文菜单,您可以在其中选择并运行此服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM