1

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

这是我的代码(它真的没有合法的功能,我只是想尝试不同的东西来看看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:".

我没有错吗?

4

1 回答 1

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
于 2013-03-14T16:50:16.930 回答