• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to determine whether launched automatically at login
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to determine whether launched automatically at login


  • Subject: Re: How to determine whether launched automatically at login
  • From: kai <email@hidden>
  • Date: Sun, 21 Aug 2005 01:04:26 +0100


On 16 Aug 2005, at 04:14, Jeff Porten wrote:

On Aug 3, 2005, at 1:56 PM, Laine Lee wrote:

How can I configure my Applescript application to determine at run time
whether it was launched automatically as a login item or whether it was
launched arbitrarily by the user? Thanks.

This might be good enough for government work....

Upon login, one of the first processes to start is loginwindow. You can get the launch time of loginwindow by parsing the results of

do shell script "ps -uxww | grep loginwindow"

Or, to avoid the need to parse data, simply get the creation/ modification date of the login window.


A login startup item will almost certainly show a launch time within a minute or two of loginwindow's. Arbitrarily launched applications will be later... usually. The caveat is because I note that I launched Script Editor and Safari within two minutes after loginwindow, and those were both manual. But maybe I'm quicker on the trigger than your users.... At the very least, this gives you a heuristic for a good guess.

The apps I tested returned a launch time of between 7 and 40 seconds. (The figure might depend on machine, OS version and what other apps are launched - not to mention the size of the application in question.)


A perfect algorithm just occurred to me, but it's would take some serious geek fu to implement. In pseudocode, the following would be part of your application:

set loginFile to plistWhichContainsUserStartupItems
set userItemCount to my parseToCountStartupItems(loginFile)
set userItemCount to userItemCount + 5 -- loginwindow, Dock, SystemUIServer, Finder, System Events; YMMV if you're not using Tiger


set currAppCount to do shell script "ps -ux | grep '.app' | wc -l" -- counts currently running apps

That returns " 1" here (mainly because the longer lines are truncated to the first 80 characters - and the removed section is what usually contains the ".app" part).


Using <do shell script "ps -uwx | grep '.app' | wc -l"> gets closer, but appears to include stuff like "sh -c ps -uwx | grep '.app'", "grep .app", etc. (the processes invoked by the shell script). Wouldn't it be simpler to get the current application processes directly from, say, System Events - and then calculate accordingly (again, avoiding the need to parse data)?

----------------

tell application "System Events" to count application processes

----------------

if currAppCount ≤ userItemCount then
-- fewer apps running total than will launch at startup, hence this is a startup item
else
-- I was run manually sometime later
end if

I can't help thinking that a method based solely on counting apps leaves itself open to a number of gotchas. For example, if the app is initially launched at login, and the user then quits it to manually re-launch it later, the algorithm would still indicate that it was launched at login. And if, in the future, Apple decides that one process more or less should be launched at login by default, that could also break the script.


The following suggestion attempts to address some of these issues:

------------------

property prevLogin : missing value
property launchTime : 100 (* max seconds to launch app: adjust as necessary *)


on launchedAtLogin()
set currLogin to (info for POSIX file (POSIX path of (path to preferences ¬
from local domain) & "com.apple.loginwindow.plist"))'s creation date
tell prevLogin to if it is currLogin then
return false
else
set prevLogin to currLogin
if it is missing value then return ((my (current date)) - currLogin) < launchTime
end if
set loginData to read POSIX file (POSIX path of (path to preferences) & "loginwindow.plist")
considering case
tell POSIX path of (path to me) to if it ends with "/" then
text 2 thru -2 is in loginData
else
text 2 thru -1 is in loginData
end if
end considering
end launchedAtLogin


if launchedAtLogin() then
    display dialog "Launched at login."
else
    display dialog "Launched manually."
end if

------------------

---
kai


_______________________________________________ Do not post admin requests to the list. They will be ignored. Applescript-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: How to determine whether launched automatically at login
      • From: kai <email@hidden>
References: 
 >How to determine whether launched automatically at login (From: Laine Lee <email@hidden>)
 >Re: How to determine whether launched automatically at login (From: Jeff Porten <email@hidden>)

  • Prev by Date: Re: SetVisibleInTiger
  • Next by Date: Re: SetVisibleInTiger
  • Previous by thread: Re: How to determine whether launched automatically at login
  • Next by thread: Re: How to determine whether launched automatically at login
  • Index(es):
    • Date
    • Thread