【问题标题】:Can't make Unix ID of Process into text?无法将进程的 Unix ID 转换为文本?
【发布时间】:2013-12-05 00:17:47
【问题描述】:

我有一个带有故障数字化板的 modbook。在我可以重新屏蔽导致故障的电缆之前,我只想关闭数字化仪。我找到了一个教我一些代码的页面,我昨晚成功使用了它。但是,重新启动后,它就不再起作用了:

脚本

tell application "System Events"
    set PTD to (unix id of process "PenTabletDriver") as text
    do shell script "kill -STOP " & quoted form of (PTD)
end tell

错误信息

错误 "Can't make «class idux» of «class prcs» \"PenTabletDriver\" of 应用程序“系统事件”输入文本。”数字 -1700 来自 «class prcs» "PenTabletDriver" 的 «class idux» 到文本

我可以修改代码来解决这个问题吗?

PS: 我已经阅读了this post,虽然它很相似,但我不明白它如何应用于我的问题。

【问题讨论】:

  • 代码没有问题。您收到错误是因为“系统事件”无法找到该进程。

标签: shell text applescript kill-process


【解决方案1】:

生命太短暂,不能与 AppleScript 混为一谈。尝试在终端提示符下运行以下命令:

pkill -STOP PenTabletDriver

此外,请检查您的登录项,以查看驱动程序是否在您每次登录时自动启动。(不过,更有可能的是,它被配置为在启动时通过 launchd 启动。)

【讨论】:

    【解决方案2】:

    您可以将以下脚本保存在一个小程序中:

    set shellStr to "pkill -STOP PenTabletDriver"
    do shell script shellStr
    

    并在需要时运行它。

    【讨论】:

      【解决方案3】:

      我认为其他答案很重要,shell 命令既好又快。但是,如果你必须在 AppleScript 中使用它,这似乎对我有用....

      tell application "System Events"
          set PTD to (unix id of process "iTunes")
          do shell script "kill -STOP " & quoted form of (PTD as text)
      end tell
      

      结果

      tell application "System Events"
          get unix id of process "iTunes"
              --> 37987
          do shell script "kill -STOP '37987'"
              --> error number -10004
      end tell
      tell current application
          do shell script "kill -STOP '37987'"
              --> ""
      end tell
      

      进程 ID 只是一个数字,因此无需引用它...

      tell application "System Events"
          set PTD to (unix id of process "iTunes")
          do shell script "kill -STOP " & PTD
      end tell
      

      上面的代码就足够了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 2014-11-25
        相关资源
        最近更新 更多