【问题标题】:Get folder path with applescript reuse it in a .sh file使用applescript获取文件夹路径在.sh文件中重用它
【发布时间】:2019-07-03 17:52:39
【问题描述】:

我正在尝试编写一个可供多个用户使用的工作流程。用户需要指定保管箱文件夹的位置,该路径需要在 shell 和 applescript 中重用。

工作流程的目标是自动设置监视文件更改的受监视文件夹(在 launchd 的帮助下)。当检测到更改时,应该触发一个 shell 脚本,该脚本运行:npm run generate in the parent of the watching folder。

我设法使用 .sh 文件和本地路径创建了工作流,但现在我想对其进行更新,因此运行脚本的用户需要选择路径,因为这些路径因用户而异。查看工作流程的步骤

  1. 选择保管箱文件夹位置
  2. 在该文件夹中创建可执行的 shell 脚本文件
  3. 向该文件添加代码

3a:添加使用所选文件夹路径运行 npm 命令的 applescript
3b:在用户 LanchAgents 文件夹中创建一个启动文件(仅限 Mac 用户)
3c:使用所选文件夹的路径将内容添加到该启动文件

  1. 启动启动文件。
    touch ~/Dropbox\ \(Folder\ name\)/folder/generate-icon-script.sh && echo "
    osascript -e 'tell application \"Terminal\"
            do script \"cd ~/Dropbox\\\ \\\(Folder\\\ name\\\)/folder/ && /usr/local/bin/npm run generate\"
            delay 10
            quit
        end tell'"  >> ~/Dropbox\ \(Folder\ name\)/folder/generate-icon-script.sh 
    && chmod a+x ~/Dropbox\ \(Folder\ name\)/folder/generate-icon-script.sh 
    && touch ~/Library/LaunchAgents/com.icons.daemon.plist 
    && echo "

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
            <key>Label</key>
            <string>com.icons.daemon.plist</string>
            <key>ProgramArguments</key>
            <array>
                <string>sh</string>
                <string>-c</string>
                <string>~/Dropbox\\ \\(Folder\\ name\\)//folder/generate-icon-script.sh</string>
            </array>
            <array>
                <string>~/Dropbox (Folder name)/folder/input</string>
            </array>
        </dict>
    </plist>
    " >> ~/Library/LaunchAgents/com.icons.daemon.plist &&
    launchctl load ~/Library/LaunchAgents/com.icons.daemon.plist

为了让用户确定路径,我创建了一个提示用户选择保管箱文件夹的正确位置的小程序。我设法在 Dropbox 文件夹中创建了 .sh 文件并将一些内容回显给它。我面临的问题是我保存的路径不能在终端中使用。

set myLocation to (choose folder with prompt "Choose the location to the Icons Folder") # Get location to save the file
set myLocationPOSIX to the POSIX path of myLocation
do shell script "touch " & quoted form of myLocationPOSIX & "/test.sh"
do shell script "chmod a+x " & quoted form of myLocationPOSIX & "/test.sh"
do shell script "echo cd " & quoted form of myLocationPOSIX & " \\&\\& /usr/local/bin/npm run generate >> " & quoted form of myLocationPOSIX & "test.sh"

如何保存路径以便在我的工作流程中使用它?任何帮助都将得到极大的认可!

我希望 echo 的输出是: cd Users/name/Dropbox\\ \\(Folder\\ name\\ )/folder/ &amp;&amp; /usr/local/bin/npm run generate

但我得到: cd /Users/name/Dropbox (Folder name)/folder/ &amp;&amp; /usr/local/bin/npm run generate

任何帮助将不胜感激!

【问题讨论】:

  • quoted form of 引用整个字符串,它不会转义单个字符。
  • brdnb - 1) 您在 com.icons.daemon.plist 文件中指定要查看的文件夹的路径名更改,(即您要监视添加、移动或删除的任何新文件/文件夹的文件夹)?我在任何地方都看不到&lt;key&gt;WatchPaths&lt;/key&gt;2) 您的 .plist 中当前的 &lt;string&gt;~/Dropbox (Folder name)/folder/input&lt;/string&gt; 部分是什么? 3) AppleScript(即提示用户选择Dropbox文件夹的正确位置的那个)是否也会在用户选择文件夹后调用shell脚本?
  • @RobC 认为我不小心删除了 Watchpath 键。通过下面的解决方案搞定了!
  • 很高兴您找到了解决方案 - 尽管不是一个好的解决方案。顺便说一句,您示例中的 generate-icon-script.sh 文件似乎没有必要……您可以从 .plist 文件中运行 npm run generate 吗?另外,我不确定您所说的 “监视文件更改的受监控文件夹”是什么意思,我认为您可能指的是文件被修改时(即某人编辑和现有文件时),但我现在意识到您只希望在有人从目标文件夹中添加、移动或删除文件时调用 npm run ...
  • @RobC 实际上确实会更好。我们说话时我正在尝试,但我无法运行多个命令。有什么想法吗?我已经用新的 launchd 文件更新了我的原始帖子。

标签: bash shell applescript


【解决方案1】:

我认为你想要的 shell 是这样的:

/Users/name/Dropbox\ (Folder\ name)/folder/

或者这个:

'/Users/name/Dropbox (Folder name)/folder/'

两者都会正确解析。你现在得到的是后者,通过quoted form of myLocationPOSIX。它应该可以工作,即使你没有看到你所期望的。使用文本项目分隔符可以获得前者是可行的,但它有点像 PITA:

set tid to text item delimiters
set text item delimiters to " "
set piecesList to text items of myLocationPOSIX
set text item delimiters to "\\ "
set newLocationPath to piecesList as text
set text item delimiters to tid 

该代码在空格处拆分位置路径,然后使用“\”再次将其连接在一起。完成了工作,但它并不漂亮......

是否有某些特定原因需要转义单个空格而不是引用整个路径?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 2014-09-22
    相关资源
    最近更新 更多