Re: Restarting an app -- AppleScript 'launch' in a detached shell
Re: Restarting an app -- AppleScript 'launch' in a detached shell
- Subject: Re: Restarting an app -- AppleScript 'launch' in a detached shell
- From: Steve Cronin <email@hidden>
- Date: Mon, 04 Aug 2008 01:15:24 -0500
Andrew;
OK for the sake of the group I will parade my ignorance so that
perhaps others can learn too...
First I do understand your point that using AppleScript is using an
unnecessary high-level tool when a lower-level tool will do!
Thanks for keeping me on-point there!! Good old-school unix thinking
- always a good thing.
I have 4 questions
1) Given that it is higher-level than necessary, I would still
appreciate understanding why it doesn't work.
I revamped the '&' and added it to the end of the osaxscript command
instead of passing as argument -> no change..
2) In the script you cited, I don't understand the 'if(fork() ||
fork())' conditional test. Can you clarify this statement?
3) Why are you using 'execl' and not 'exec'?
4) When you say "...launch this with a single argument..." I' not
sure of exactly what you mean. Launch how?
I tried creating a text file with your script as the contents
(Restart.unixscript)
//enclose the paths in single quotes because there are potentially
spaces in either path (eg 'Application Support')
NSString *scriptPath = [NSString stringWithFormat:@"'%@'",[[self
scriptsFolderPath]
stringByAppendingPathComponent:@"Restart.unixscript"]];
NSString *myPath = [NSString stringWithFormat:@"'%@'",[[NSBundle
mainBundle] bundlePath]];
NSFileHandle *stdErrors = [NSFileHandle
fileHandleForWritingAtPath:@"/Users/steve/Desktop/RestartErrors.txt"];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:scriptPath, myPath,
nil]]; // no -c means that first parameter is file of commands
[task setStandardError:stdErrors];
[task setStandardOutput:stdErrors];
[task launch];
[[NSApplication sharedApplication] terminate:self];
When I do this I get the following in stdErrors:
/bin/sh: '/Users/steve/Library/Application Support/XYZ/Scripts/
Restart.unixscript': No such file or directory
NOTE: enlosing in double quote doesn't change the result; nor
obviously does not enclosing the paths in a delimiter at all
Seems like I'm missing something....
Thank-you for your patience on this!
Steve
On Aug 3, 2008, at 4:12 PM, Andrew Farmer wrote:
On 03 Aug 08, at 14:00, Steve Cronin wrote:
I am trying to simply restart my app. To my dismay, I cannot find
a way to do this in Cocoa.
Please enlighten me if I have failed to understand something!!
After perusing the archives, what makes the most sense to me is to
launch a detached shell that executes a simple AppleScript.
The script has a delay of 2 seconds and then simply does a 'launch'.
You're overcomplicating things.
int main(int argc, char **argv) {
if(fork() || fork()) exit(0);
sleep(2);
execl(argv[1], argv[1], NULL);
exit(1);
}
Launch this with a single argument - the path to your application's
executable. It'll background itself, sleep two seconds, and exec
your application.
(Standard disclaimer: typed out in Mail, untested.)
I've got the script working like a champ in a straight terminal
session:
> /usr/bin/osascript '/Users/steve/Desktop/Restart.script'
So now:
NSString *cmdString = [NSString stringWithFormat:@"/usr/bin/
osascript '%@'",[[self scriptsFolderPath
stringByAppendingPathComponent:@"Restart.script"]];
NSTask *task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:@"-c", cmdString, @"
&", nil]]; //the '&' detaches the shell, yes?
& only detaches a command when it's part of a shell command. You're
passing it as an argument to /bin/sh here, which is subtly different.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden