在列表中存储字符串时,Applescript返回错误

时间:2013-03-14 16:41:29

标签: macos applescript

我编写了一个简单的脚本,可以找出当前在机器上运行的活动进程数,并将每个进程的路径作为字符串输出到数组中。

这是我的代码(它实际上没有合法的功能,我只是想尝试不同的东西来看看AppleScript是如何工作的):

tell application "System Events"
  set activeProcess to number of process
  set paths to {0}
  repeat with n from 1 to activeProcess
    set last item of list paths to (file of process n as string)
  end repeat
end tell

这是我点击跑步时错误的AppleScript编辑器返回的错误:

System Events got an error: Can’t set list {0} to "Macintosh HD:System:Library:CoreServices:loginwindow.app:".

我没有错?

1 个答案:

答案 0 :(得分:2)

尝试:

tell application "System Events" to set myprocess to files of processes

set AppleScript's text item delimiters to linefeed
tell application "System Events" to set myprocess to paragraphs of (files of processes as text)
set AppleScript's text item delimiters to {""}

您可以从重复循环中构建一个列表,如下所示:

set paths to {}
tell application "System Events"
    set activeProcess to processes
    repeat with n from 1 to count activeProcess
        set end of paths to (file of item n of activeProcess as text)
    end repeat
end tell

或者像这样:

set paths to {}
tell application "System Events"
    set activeProcess to processes
    repeat with aProcess in activeProcess
        set end of paths to (file of aProcess as text)
    end repeat
end tell