0

I have been trying to run a script on a recurring basis via an apple service.

Most answers I have seen have been are for 'stay-open' apps - whilst this possibly works as a solution and I have glanced information about hiding from the dock/application switcher this isn't an elegant solution for me I don't feel. Advice in the comments of this answer says that the 'on idle' call of a script only works when it is saved as a 'stay-open application', but is there an automator-native way of getting a service to execute it's script at an interval?

With the answer linked above the launchd option described is probably the best solution so really on an academic level I am just curious whether this is possible with an Automator service running some applescript

If it helps frame the context of the question any better here is the code I want to run:

on logSomething(message)
    tell application "Finder"
        tell me to log message
    end tell
end logSomething

on getCurrentApps()
    tell application "System Events"
        set processList to get the name of every process where background only is false
        return processList
    end tell
end getCurrentApps

on storeAppName(thePath, applicationName)
    --this creates an object vehicle in which to store data
    script theData
        property appName : applicationName
    end script

    store script theData in file thePath replacing yes
    return theData's appName
end storeAppName

on readAppName(thePath)
    tell application "System Events"
        if exists file thePath then
            set theScript to load script file thePath
            return theScript's appName
        else
            return false
        end if
    end tell
end readAppName

on idle
    set dataPath to "tmp:unhide_windows_data.scpt"
    set storedProcess to readAppName(dataPath)

    tell application "System Events"
        set currentProcess to first process where it is frontmost
        -- my logSomething("currentProcess windows: " & (the name of windows of the currentProcess))
        if currentProcess's name is equal to storedProcess then
            --report that the stored and observed process name are the same
            display dialog "They are the same"
            my logSomething("Is the current process visible? " & (visible of currentProcess as text))
        else
            --report that the stored and observed process name are not the same
            display dialog "They are not the same, storedProcess: " & storedProcess & ", currentProcess: " & currentProcess's name
            my storeAppName(dataPath, currentProcess's name)
            set visible of currentProcess to true
        end if
    end tell

    readAppName(dataPath)
    return 5
end idle
  • IMO When the return is only 5 seconds there is going to be issues if the intent is to start it an keep it running indefinitely, but I'd do it with launchd and have your code executed by osascript. Save the code as a plain text file using the #!/usr/bin/osascript shebang and make the file executable. Using Automator is more resource intensive then a stay-open AppleScript app. – user3439894 Aug 17 at 13:21
  • When you say plain text do you mean '.txt'? – Scott Anderson Aug 17 at 13:23
  • Plain text is plain text whether or not it has an extension. I personally do not use extensions on files I make executable that are going to run AppleScript code using the #!/usr/bin/osascript shebang. – user3439894 Aug 17 at 13:36
  • I have done what you specified, would the plist program arguments now just look like <key>ProgramArguments</key> <array> <string>~/Tech/langs/Applescript/Unhide_Windows</string> </array> ? (the path is the path to the file with the shebang) When I run it just by entering the path in the terminal (emulating the plist action) it doesn't give anything to the terminal wheras when I run the .scpt file with osascript it gives log statements to stdout – Scott Anderson Aug 17 at 14:29
  • I did not read your script verbatim, or test it, only looked at the low return value. I'm not at my Mac to check and test until late this evening, sorry. You may have to invoke bash -c /path/to/script when using launchd for it to work from there. – user3439894 Aug 17 at 15:09

You must log in to answer this question.

Browse other questions tagged .