The following code:
dim sh as Shell = New Shell
dim command, process, result as string
process = “Preview”
// command = “ls -l”
command = “ps -ef | grep " + process + " | grep -v grep”
sh.Execute(command)
result=sh.Result
yields the result:
ps: illegal option – f
usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid…]]
[-u]
[-p pid[,pid…]] [-t tty[,tty…]] [-U user[,user…]]
ps [-L]
Note that the commented command “ls -l” runs fine. Also the ‘problem’ command “ps -ef | grep Preview | grep -v grep” also runs fine in Terminal, returning:
8651171 2768 287 0 8:38PM ?? 0:08.36 /Applications/Preview.app/Contents/MacOS/Preview -psn_0_1306943
Any ideas? The ultimate goal is to get the pid of the given process, in this case “Preview” (2768).
I just copied and pasted your code into a test project and it worked just fine, so there is something else in play here. Try changing the call to:
command = "/bin/ps -ef | /usr/bin/grep " + process + " | /usr/bin/grep -v grep"
You can also do this with this AppleScript:
if running of application "Preview" then
tell application "System Events"
set p to first process whose name is "Preview"
set appID to unix id of p
end tell
end if
A little safer is this AppleScript:
if running of application id "com.apple.Preview" then
tell application "System Events"
set p to first process whose bundle identifier is "com.apple.Preview"
set appID to unix id of p
end tell
end if
So, the code does work if in a desktop app, but this needs to be a console app. The “/bin” version also errors.
Also, if I save an Applescript & drag it into the project, then call it, I get the following error:
dyld: lazy symbol binding failed: Symbol not found: _RuntimeScriptFunction
Referenced from: /Users/rr163073/Desktop/MyConsoleApp.debug/MyConsoleApp.debug
Expected in: /Users/rr163073/Desktop/MyConsoleApp.debug/MyConsoleApp.debug Libs/rbframework.dylib
dyld: Symbol not found: _RuntimeScriptFunction
Referenced from: /Users/rr163073/Desktop/MyConsoleApp.debug/MyConsoleApp.debug
Expected in: /Users/rr163073/Desktop/MyConsoleApp.debug/MyConsoleApp.debug Libs/rbframework.dylib
Trace/BPT trap: 5
I just tried your original code in a console app and it works here too. I think we need more information.
This was a console application. Built it as a windowless desktop app, and it works fine. Thanks!
[quote=41646:@Rick Plummer]Also, if I save an Applescript & drag it into the project, then call it, I get the following error:
dyld: lazy symbol binding failed: Symbol not found: _RuntimeScriptFunction
Referenced from: /Users/rr163073/Desktop/MyConsoleApp.debug/MyConsoleApp.debug
Expected in: /Users/rr163073/Desktop/MyConsoleApp.debug/MyConsoleApp.debug Libs/rbframework.dylib
dyld: Symbol not found: _RuntimeScriptFunction
Referenced from: /Users/rr163073/Desktop/MyConsoleApp.debug/MyConsoleApp.debug
Expected in: /Users/rr163073/Desktop/MyConsoleApp.debug/MyConsoleApp.debug Libs/rbframework.dylib[/quote]
How do you call AppleScript from your project ?
If you add the script to the project, simply call it like any other method/function (passing any necessary args).
Make sure the ‘process’ is quoted. Maybe that is the culprit.
BTW MBS has ProcessMBS which does this with ease.