Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Autostart of application

Is it possible to add Titanium Desktop application to system startup regardless it is MacOS, Linux or Windows?

— asked October 7th 2010 by Adam Stankiewicz
  • desktop
0 Comments

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.

    — answered July 4th 2011 by David Rugendyke
    permalink
    2 Comments
    • I managed to optimise my code above and fix an issue with the startup shortcut not being created on some systems with higher security levels. Use this code below instead.

       // Windows startup shortcut 
                    if(Titanium.platform == "win32") {
      
                          // Used by the windows commands to get the startup folder location
                          var startupRegKeyWin = ["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 regString = null; 
                          // Set the process
                          var process = Titanium.Process.createProcess(startupRegKeyWin);
      
                          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')) {
                                  // Create the shortcut in the startup folder now that we have the path
                                  var appPathShortcut = startupFolderPath+'\\'+Titanium.App.getName()+'.lnk'
                                  var file = Titanium.Filesystem.getFile(appPathExe);
                                  file.createShortcut(appPathShortcut);
                              }
                          });
      
                         // Fire the process to find the reg key
                         process.launch();
                      }
      

      — commented July 7th 2011 by David Rugendyke
    • does not work for me…

      — commented July 1st 2012 by Alexander Frischbutter
  • 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….

    — answered May 14th 2011 by Chris Moore
    permalink
    0 Comments
  • Any update on this? Particularly for OSX?

    — answered March 4th 2012 by Anton Zaroutski
    permalink
    0 Comments
  • 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

    — answered March 26th 2012 by Jean-Baptiste Armanet
    permalink
    1 Comment
    • Sorry, the second code block for this example should be :

      var temp = Titanium.App.getPath().split('MyApp.app');
      var appPath = temp[0]+'MyApp.app';
      

      — commented April 2nd 2012 by Jean-Baptiste Armanet
  • 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?

    — answered October 7th 2010 by Roger Chapman
    permalink
    1 Comment
    • Type of my application requires this kind of behavior. Is there any tutorial how to cover the most popular operating systems (giving, that we want to affect a Titanium application)?

      — commented October 7th 2010 by Adam Stankiewicz
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.