Applescript to disable scheduled shutdown if #%n app is running? - applescript

Is there an Applescript somewhere that can tell the Energy Saver shutdown scheduler in OSX 10.8 "don't shut down the computer if #%n app is running?" I use the Energy Saver scheduler to shut down my machine in the evenings and power back up in the mornings, but sometimes I need to let an app run overnight.
I know about Caffeine, but I want something automated, hence the script - I want the script to tell the Energy Scheduler, "Hey, #%n app is running, don't shut down tonight" without requiring me to do anything.
Thanks!

One way you can do this is not use the Energy Saver shutdown scheduler at all, and instead use the Calendar application to schedule when to run this little script to conditionally shutdown the system.
In this example, I have set this up for iTunes, but you can change this to any application you require.
Use the Applescript Editor to create and save a script like this:
set myapp to "iTunes"
if application myapp is not running then
tell application "Finder" to shut down
end if
Open the Calendar application and set up a new event which occurs whenever you want the scheduled shutdown to occur e.g. every day at 9pm. In the "New Event" box, you can change the "alert" field to "Open File", then change the file to be opened to the script that you just saved.
The script should run when you have scheduled it, and only shutdown the system if iTunes (or whichever application you want) is not running.
Note that tell application "Finder" to shut down will do a "careful" shutdown - i.e. if there are any other applications which don't want to be shutdown, then the shutdown will be cancelled.

Related

Block application on mac

is there a way to run a simple tell application quit, when a specific application is opened.
For example, if I open spotify, is there away to trigger a script to quit the app.
Thanks!
is there a way to run a simple tell application quit, when a specific application is opened.
Short Answer: Yes
Longer Answer:
Here are a couple of ways that come to mind...
There is a paid application called EventsScripts that among the many events it can react to, one category is Application Events which contains, Application activated, Application deactivated, Application will launch, Application launched and Application quit.
EventsScripts works with both AppleScript scripts and shell scripts.
Have a look at EventScripts. At the time of this post, it's $5.99 at the US App Store, but a free demo is downloadable from the developers website.
Note: I am not affiliated with the developer of EventScripts, just a satisfied user of the product.
Example AppleScript code:
on run eventArgs
set thisTrigger to (trigger of eventArgs)
if thisTrigger is "Application launched" then
set appName to |applicationName| of eventArgs
if appName is "Spotify" then
tell application appName to quit
end if
end if
end run
A free alternative is Hammerspoon, although one may find it is not as easy to implement and use as e.g. EventsScripts.
Here is an example of the code used to watch for the target application has launched and then close it using AppleScript code:
Example Lua code:
function applicationWatcher(appName, eventType)
if (eventType == hs.application.watcher.launched) then
if (appName == "Spotify") then
hs.applescript('tell application "Spotify" to quit')
end
end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()
This would be placed in the ~/.hammerspoon/init.lua file and with Hammerspoon running in the background, when the target application is launched, it is told to quit via AppleScript.
Note: I am not affiliated with the developer of Hammerspoon, just a satisfied user of the product.

applicationShouldTerminate: not called when system restarts

I have to run some code when the app is about to terminate. applicationShouldTerminate: runs when quit is selected from the menu or when I press Cmd Q but not when I restart the mac.
Is there a way to force applicationShouldTerminate when a user tries to restart the mac? Or is it another function being called in this scenario?
Your app might have Sudden Termination enabled.
macOS 10.6 and later includes a mechanism that allows the system to log out or shut down more quickly by, whenever possible, killing applications instead of requesting that they quit themselves.
...
Debugging tip
You can determine the value of the sudden termination using the following LLDB command.
print (long)[[NSClassFromString(#"NSProcessInfo") processInfo] _suddenTerminationDisablingCount]

Why don't some programs appear on the task manager startup tab?

Many applications start at startup, however, some of them do not appear on the task manager startup tab. What is that due to?
Is there any way to do this with a program, for example, spotify?
What do I need to do in order for a program to start at startup, but not showing in the startup applications tab?
Setting it in HKCU/Microsoft/Windows/CurrentVersion/Run doesn't seem to work, as it starts, but still shows on the mentioned tab.
Thank you in advance.
Its mostly a factor of how the program itself is written. If its written to run as a service, or as a System Tray application, or otherwise.
I know there are wrappers for running any exe as a service NSSM being the main one I have experience with (but this is mostly for when there is going to be NO user interaction)
I do not know if there is anything that can allow an application to run in the system tray only, not in the taskbar, if it doesn't support it.
But since Spotify does support running minimized to the tray, it does seem like there are some ways to "start spotify minimized", Spotify or other applications might have command line options or other settings to tell them to start "hidden"

Run Apple Script before application start?

Was wondering if there is any way to run a small piece of apple script when I launch an application on OS X?
I have a hard-drive which need to be mounted in order for the app to run right. I did this apple script already:
on run
try
do shell script "diskutil mountDisk disk2"
delay 3
tell application "Application"
run
end tell
on error
tell application "Application"
quit
end tell
set question to display dialog "Error." buttons {"OK"}
set answer to button returned of question
if answer is equal to "OK" then
close
end if
end try
end run
But even tho this works great and the hard-drive spins up before the app starts.
I would like to add the diskutil mountDisk disk2 and delay directly in front of the real application when its starting so I can double click files for that app say in my downloads folder. Which will run the script before it opens the clicked file in the app. So I wont have to start the application with another application each time i download a new file for it. Is this even possible?
Shortly:
It's not possible unless the application itself supports some inhook and outhook functionality.

Use applescript to quit iTunes for all users

I have iTunes on set up of my iMac to share an iTunes library between two different users. This means that if one person forgets to quit iTunes, the other user has to log in to the other account and quit itunes before they can use it on their own account.
So is there a way to use applescript to quit an application for all users? I know it's easy to tell it to quit an application for the current user, but haven't been able to figure out if it is possible to have it quit another user's instance of that application.
You can simply use an shell (terminal) command.
You can either enter killall iTunes in the terminal, or do shell command "killall iTunes" in Applescript.
It doesn't sound very healthy, but it's actually just a 'Forced Quit'.
I'm not on my Mac at the moment, so cannot look into this further.
But it may work better if you turn fast user switching off. Doing so will not stop two or more users logging in but will hide the GUI for it.
The create a Automator service or app or a Applecript/app or script. To run this AppleScript code.
tell application "iTunes" to quit
do shell script "'/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession' -suspend"
This code should quit iTunes and than show the login screen. Where the next user can login.
The other user will not be logged out.
Since I can't respond yet, I'll just do it this way:
Markhunte:
Make that
tell application "iTunes" to quit
do shell script "'/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession' -suspend"

Resources