【问题标题】:Tell an AppleScript to Restart Itself告诉 AppleScript 自行重启
【发布时间】:2015-11-13 11:16:32
【问题描述】:

我有一个 AppleScript 保存为应用程序。首次运行时,它会询问用户是否要将其移动到 Applications 文件夹。我想做的是,在它被移动后,让脚本自行退出然后重新打开。

显然我不能说

tell me to quit
tell me to activate

...因为它会在quit 命令之后停止运行。

有什么建议吗?

【问题讨论】:

    标签: applescript application-restart


    【解决方案1】:

    只需从脚本内部运行脚本,并确保通过返回终止当前运行(如果它是脚本的最后一行,则可以跳过实际的返回命令

    -- do stuff
    display dialog "Here I am again"
    
    -- set alias to the script
    -- run the script
    set myScript to path to me
    run script myScript
    -- end current iteration
    return
    

    您可以通过取消对话框来中断该脚本,但您可能需要设置一个条件来检查是否再次运行该脚本。

    【讨论】:

    • 我收到Error -43 Folder does not exist。我猜path to me 仍然指向旧路径,即脚本启动时的路径。
    • 是的,无论您将文件移动到何处,都需要使用该路径。我没有为你写完整的剧本,我只是向你展示了如何完成你不知道的部分。您需要执行以下操作:将 myScript 设置为(应用程序文件夹的路径作为字符串)和“myScriptName”
    【解决方案2】:

    我会这样做。基本上,您检查应用程序是否从 Applications 文件夹运行。如果不是,请将其移到那里,打开另一个实例,然后退出。似乎完美无缺。开始的激活是因为应用程序似乎并不总是将自己移动到前台:

    --incase the application doesn't do this automagically
    activate
    set my_path to POSIX path of (path to me)
    if my_path does not start with "/Applications/" then
        set new_path to "/Applications/" & quoted form of (my name & ".app")
        --"mv" wont move the application into the new location if it exists
        try
            do shell script "rm -rf " & new_path
        end try
        do shell script "mv -f " & quoted form of my_path & " " & new_path
        do shell script "open -n " & new_path & " &> /dev/null &"
        quit
    end if
    

    【讨论】:

      【解决方案3】:

      我在做什么。

      首先我通过在终端中运行以下命令启用 at (只需执行一次)

      sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist
      

      然后我有以下脚本

      display dialog "running"
      set mypath to POSIX path of (path to me)
      set lun to open for access POSIX file "/tmp/springboard" with write permission
      write "open " & mypath & linefeed to lun
      close access lun
      do shell script "at -f  /tmp/springboard +1 minute"
      quit
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多