Autostart of application
Is it possible to add Titanium Desktop application to system startup regardless it is MacOS, Linux or Windows?
5 Answers
-
Hey guys, I had the same issue with not being able to start my app when the system launched. I have however come up with working code for WinXP and Win7 that adds the app to the users startup folder and launches when the OS loads. I initially came up with a way to add it to the registry, but that only worked if you launched the app as 'administrator' and i'm pretty sure most users won't do that. Having a shortcut in the startup folder also makes it easier for the user to manage too.
Here's the code. Please note that i've used a Mootools trim function in there, if you don't use Mootools, use your frameworks equivalent to trim the white space from the string.
// Windows startup shortcut if(Titanium.platform == "win32") { // Get the startup folder path from the registry via the command line var startupRegKey = ["C:\\Windows\\System32\\cmd.exe", "/C", "REG", "QUERY", "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "/v", "Startup", "|findstr", "/i", "REG_SZ"]; // Set out app exe path and the shortcut path var appPathExe = Titanium.Filesystem.getApplicationDirectory()+'\\'+Titanium.App.getName()+'.exe'; var appPathShortcut = Titanium.Filesystem.getApplicationDirectory()+'\\'+Titanium.App.getName()+'.lnk'; // Create a shortcut from the apps exe var file = Titanium.Filesystem.getFile(appPathExe); file.createShortcut(appPathShortcut); var regString = null; // Set the process var process = Titanium.Process.createProcess(startupRegKey); process.setOnReadLine(function(data) { // Get the string from the registry that contains out startup folder path regString = data.toString().split('REG_SZ'); // Split the string on REG_SZ, this will leave our startup folder path - trim the string to remove any unecessary white space // .trim() is a Mootools method, if your using jQuery, use $.trim(regString[1]) instead var startupFolderPath = regString[1].trim(); // An extra check to make sure this is the reg key with the Startup folder if(startupFolderPath.test('Startup')) { // Copy the shortcut to the startup folder now that we have the path var file = Titanium.Filesystem.getFile(appPathShortcut); Titanium.Filesystem.asyncCopy(file, startupFolderPath, function() { // Now remove the shortcut from out installation dir that we copied file.deleteFile(); }); } }); // Fire the process to find the reg key process.launch(); }
Goodluck! Hope it helps someone else out.
-
Any update on this? Theres a lot of apps that need this capability and it would be great to have a tutorial on it, or something built into the Titanium installer….
-
Any update on this? Particularly for OSX?
-
Hi guys, I found a way for OS X (at least it works on 10.7).
I found the command-line required to make it BUT I can only fire it from PHP… although firing it from Ti.Process.createProcess would be awsome as the final App would not need to embed the 30 to 40 MB PHP stuff !!
But I'm on it for days, and after tons of tries I can't figure how to achieve this with createProcess :-(However, here is the trick for registering your app at OS X session Login for the user account : the idea is to register your app path into longinwindow.plist
The PHP function :
function startupRegister($appPath) { exec("/usr/bin/defaults write ~/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -array-add \"<dict><key>Path</key><string>$appPath</string></dict>\""); }
Call this function from wherever you want in your .js file :
At first get your app path (let's say your app is called "MyApp" :
var temp = Titanium.App.getPath().split('MyApp.app'); var appPath = temp[0]+'MenuBells.app';
Then send it to PHP :
startupRegister(appPath);
Once again, including PHP and its 30+ MB only for that sake is quite odd… if that exec command-line could be fired via a createProcess, it would be much better :
I tried such a thing :
var echoCmd = ["/usr/bin/defaults","write","~/Library/Preferences/loginwindow","AutoLaunchedApplicationDictionary","-array-add","\"<dict><key>Path</key><string>/Applications/myApp.app</string></dict>\""]; var process = Titanium.Process.createProcess(echoCmd); process.launch();
But no chance to make it work, wherever I put the commas, or even the whole command-line as single array stack… If someone could help making this createProcess working, that would save my say !
Thanks, and hopefully this PHP trick at least will help some…
Cheers
JB
-
There are ways to add any application to the system startup, but the methods are slightly different for each OS. Why do you want to start Titanium Desktop on startup?