Use applescript to quit iTunes for all users - applescript

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"

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.

Can't Change Input Method when Launching App with Admin Privileges

I wrote a script using the Mac script editor on Mojave (10.14.15)
The script simply launches an application with Admin privileges:
on run
do shell script ¬
"/Applications/App.app/Contents/MacOS/App" user name "name" password "pw" with administrator privileges
quit
end run
I then saved this as an application and am able to launch successfully using it.
However the application that I launch (Anki study software) can not change the input setting, which means I am unable to type in any language except the default system language.
Note that making a similar application in script editor that launches without admin privileges works without issue,
ie:
on run
do shell script ¬
"/Applications/App.app/Contents/MacOS/App"
quit
end run
This seems to be the case no matter the app I launch, I also tried launching Sublime Text and the same thing occurs.
The issue also happens when using sudo to launch an app from terminal. So it definitely seems to be an issue with trying to launch an app as root.
I am new to working with AppleScript and MacOS. It seems the fault is with launching the application with admin privileges though. Any help would be greatly appreciated.

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.

Why do other applications open, when I open a certain script on applescript?

Every time i open this specific script, other applications open with it and i haven't even clicked on run. Applications like Appstore and system information open, and they are only mentioned in the script once, halfway through:
tell application "Grab" to quit
tell application "Network Utility" to quit
tell application "System Information" to quit
tell application "Terminal" to quit
tell application "Keychain Access" to quit
tell application "Disk Utility" to quit
tell application "Bluetooth File Exchange" to quit
tell application "Boot Camp Assistant" to quit
tell application "AirPort Utility" to quit
tell application "Activity Monitor" to quit
tell application "App Store" to quit
tell application "iTunes" to quit
Why does this happen?
When you open the script in script editor, it will open all necessary apps in order to load their library terminology. It has always operated this way. It's especially annoying when you open someone's script that has tell blocks to apps that you don't have installed.
This does not answer the question because you already have the answer.
But, here's how to not open these applications when you want to edit the script.
Just put the application name in a variable.
set appNames to {"Grab", "Network Utility", "System Information", "Terminal", "Keychain Access", "Disk Utility", "Bluetooth File Exchange", "Boot Camp Assistant", "AirPort Utility", "Activity Monitor", "App Store", "iTunes"}
repeat with tName in appNames
quit application tName
end repeat
An AppleScript can’t tell an app to do something if that app is not running. So if a script includes a “tell” statement that targets a specific app, that app has to run along with the script. It’s the same as invoking a PHP library function in a PHP script. If you use a strpos function in a PHP script, then that PHP script can’t run without strpos also running.
This is by design because the purpose of AppleScript is to create workflows that include multiple apps working in concert on a specific task. AppleScript itself has almost no built-in functionality — your AppleScripts get their functionality from Mac apps, Unix apps, and network services.
In the script you’re working with, you can comment-out those lines (put two dashes in front of each line) if you know that those apps will not be running, because all it is doing there is telling those apps to Quit. That AppleScript is not using those apps for any real functionality.

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

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.

Resources