Block application on mac - macos

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.

Related

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.

Mac OS X: interacting with an application programmatically

I am working on a project where I need to call methods on an existing application (my own) and use some of its functionality. For e.g. my application ThunderBolt runs on Mac OS X 10.10. It also provides a dictionary of events that can be called externally through Apple Script or some other way that I don't know yet.
My question is what are the different (and better) ways of interacting with an application programmatically on Mac OSX? If I use something like the following code in Apple Script Editor:
tell application "ThunderBolt"
set open_file to (choose file with prompt "Choose the file you wish to parse")
set theContents to read open_file as data
set retPict to (image convert theContents)
end tell
then it is going to launch ThunderBolt with a splash screen and then call "image convert". This can be done via NSAppleScript but still it would launch the application and call methods/events on it.
Is it possible to somehow create an instance of (or get a pointer to) one of the class inside the application and use that? Something similar to COM or automation on a Windows system?
If you're working on OS X 10.10, you might consider taking a look at JavaScript for Automation (JXA).
With it you can apparently build methods into your app that can be invoked from client scripts written in JS (although I'm not yet familiar with the particulars of how to handle implementation of such a thing on the app side). But many of the apps that ship as part of OS X Yosemite have such APIs built in (e.g. iTunes and Finder).
Here's a great tutorial on JXA written by Alex Guyot: http://www.macstories.net/tutorials/getting-started-with-javascript-for-automation-on-yosemite/
The JXA-Cookbook repo also appears to be a nice resource: https://github.com/dtinth/JXA-Cookbook/wiki
Here's a brief example - this script makes iTunes go back one track. Try it while iTunes is playing (by putting the text into Script Editor, with the language option set to JavaScript, and hitting the Run button):
iTunes = Application('iTunes')
state = iTunes.playerState()
// Console msgs show up in the Messages tab of the bottom view:
console.log("playerState: " + state)
iTunes.backTrack()
Alternatively, you can place the code into a .js file and run it on the command line:
$ osascript itunes-backTrack.js
playerState: playing
The way you specify the 'tell application' is the best way, in my opinion.
What do you do with your app that needs to be called? Maybe some of the functionalities can be done with Applescript? It would simplify things a lot.

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.

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"

Applescript Remote Click

I'm trying to get remove mouse events working and I'm attempting to use this little script:
set machineB to "eppc://user:pw#myothermac.local"
tell application "Finder" of machine machineB
say "Hello This is so stupid"
end tell
tell application "System Events" of machine machineB
click at {100, 100}
end tell
The script say's fine, but throws the following error:
error "System Events got an error: Can’t continue click." number -1708
–1708 <reference> doesn’t understand the <commandName> message.
What am I doing wrong? I can use it to "click" on my own machine (at least it doesn't throw errors)?
[Edit]
this is related to my other question here.
You can't do this. Applescript and its tell blocks work in the context of the Mac running the application; you can send commands to the Finder on your home Mac because you are running it, but you can't send commands to the instance of Finder on a target Mac. If you are able to run an application located on the target machine from your home Mac, then the application will behave as if it is being run on the home Mac.
If you are looking to do complex remote actions, then I suggest Apple Remote Desktop or another similar method.

Resources