非常相似,但不等于: How to check in AppleScript if an app is running, without launching it - via osascript utility
我想要一个automator应用程序,它启动iterm并在vim中调用输入文件。 如果iterm已经打开(不是真的需要检查它的vim;虽然会很好!)我想拆分窗口并在拆分中打开文件。
我的尝试:
on run {input}
-- get the file path incl extention
set inputFilePath to POSIX path of input
display dialog appIsRunning("iTerm")
tell application "iTerm"
if it is running then
display dialog "yes"
tell current terminal
tell the last session
write text ",w" -- split window vertically
write text openInVim
end tell
end tell
else
display dialog "no"
set openInVim to "vim " & quoted form of inputFilePath
set text item delimiters to "/"
set parentDir to text items 1 thru -2 of inputFilePath
tell i term application "iTerm"
activate
tell current terminal
tell the last session
delay 0.1
write text "cd " & parentDir & "; clear"
write text openInVim
end tell
end tell
end tell
end if
end tell
end run
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
只需运行调用,两个显示都会给出正确的天气答案,或者应用程序是否已经全部运行! 如果我使用应用程序(删除它上面的文件)似乎,它首先启动iTerm因为一些tell语句然后总是说它已经打开了!
运行脚本并将所有内容放入“”不起作用。 对于为什么会发生这种情况或如何获得预期行为的任何建议?
答案 0 :(得分:0)
我找到了一个使用grep via shell脚本的解决方法。没有列出像vim这样的系统进程。这现在寻找vim的开放过程,而不是iTerm(无论如何都将打开)。如果它找到vim,它会拆分一个窗口(更改你的命令!),如果没有,它会启动一个新的vim,文件夹为路径,vim包含该文件:
on run {input}
-- "word 1 of myProcessInfo" is the unix id of the process
-- "word 2 of myProcessInfo" is the unix id of the parent process
-- "word 3 of myProcessInfo" is the name of the process
try
set myProcess to "Vim" -- your process name here
set myProcessInfo to do shell script ("ps -xco pid,ppid,comm | grep " & myProcess)
set isProcessRunning to 1
on error
set isProcessRunning to 0
end try
-- get the file path incl extention
set inputFilePath to POSIX path of input
if isProcessRunning = 1 then
--display dialog "vim is already running"
set openInVim to ":e " & inputFilePath as string
tell application "iTerm"
activate
tell current terminal
tell the last session
-- split vim window!
write text ":vsplit"
tell i term application "System Events" to keystroke return
write text openInVim
end tell
end tell
end tell
else
--display dialog "new iterm and vim"
set openVim to "vim " & quoted form of inputFilePath
set text item delimiters to "/"
set parentDir to text items 1 thru -2 of inputFilePath
tell application "iTerm"
activate
tell current terminal
tell the last session
delay 0.1
write text "cd " & parentDir & "; clear"
write text openVim
end tell
end tell
end tell
end if
end run
来自http://hintsforums.macworld.com/archive/index.php/t-27113.html
的allanmarcus提供的想法