Applescript Remote Click - macos

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.

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.

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.

How to prevent application from crashing when display goes to sleep in Apple Script

How can I prevent an Apple Script application from crashing whenever the computer logs out or goes to sleep?
I'm currently working on an application that sets the desktop background to the album art of the current song playing in iTunes, however if I put my display to sleep, iTunes will continue to play music but my application will crash, meaning that the desktop background is no longer updating when I return to the computer.
This is the code I'm using to set the wallpaper:
tell desktop 2
set picture to fileName
end tell
where fileName is name of the iTunes artwork of the current song, which has been extracted and saved to the desktop.
It says desktop 2 as I have two different applications for each screen, and conduct testing on my 2nd screen.
This is the error I get when running in Automator and putting the display to sleep:
The action “Run AppleScript” encountered an error.
System Events got an error: Can’t get desktop 2. Invalid index.
I am getting the same results on my primary display also.
How can I stop this script from encountering an error when the display is asleep?
Any suggestions would be greatly appreciated, as I'd love anyone who wants it to have this application.
The safest way is to check if the item exists
if exists desktop 2 then
tell desktop 2
set picture to fileName
end tell
end if
or wrap the code in a try block which ignores any error.
try
tell desktop 2
set picture to fileName
end tell
end try

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.

VMware fusion and applescript with global hotkeys

I need a terminal to a Linux vm inside my osx installation. Its a dropdown terminal and in the vm I can use F12 as a global hotkey to toggle show/hide.
The name of the dropdown terminal is yakuake, I know there is a dropdown terminal that works with osx to, but for various reasons, I need both a Linux terminal and easy access to it.
There is several things that makes this difficult.
VMware fusion doesnt have any good applescript support
I need to be able to run several vm's on the same time.
Speed
Hiding the application.
I am using iKey to get a global hotkey on my mac binded to a little oneliner:
/Library/Application\ Support/VMware\ Fusion/vmrun -T fusion -gu username -gp password runScriptInGuest "/Users/username/Documents/Virtual Machines.localized/Ubuntu.vmwarevm/Ubuntu.vmx" -interactive "/bin/bash" "/home/username/bin/toggle_yakuake"
The /home/username/bin/toggle_yakuake script is just a:
/usr/bin/xdotool key Control+Shift+Y # Which is what I bound to yakuake
This works, except its taking around 1-2 seconds, and yakuake will show up below every other windows.
Therefor I will use logic to just do this if yakuake isnt running, and then I can just hide/show it in osx to toggle it instantly.
However, I am able to show yakuake using a simple "tell application "Yakuake - Ubuntu" to activate. I am not able to hide it without hiding every other application belonging to the same vm.
It seems that every application spawned by vmware is tied together a little to tight.
Even if I find the pid of yakuake on my osx installastion, and uses it like:
tell application "System Events"
set yakuakeproc to every process whose unix id is 58518
repeat with proc in yakuakeproc
set the frontmost of proc to true
end repeat
end tell
It will hide every application belonging to that vm.
I have tried to figure out a solution for this for days now. Is there anyone that have any tips or anything? How can I get applescript to hide one specific application which is inside my vm?
The way you're starting yakukake is unnecessarily complex. You should be able to simply run the VMware proxy application, rather than using a shell script; i.e. your tell application "Yakukake - Ubuntu" to activate.
Then your only real problem is how to show and hide an application from a script. I think it is possible to do this with System Events, but it's easier to do it with my tool appswitch:
I don't have GUI Linux installed in VMware Fusion but here's an example with Windows.
To view information about a process:
% appswitch -La Notepad
PSN PID TYPE CREA NAME PATH (bundle identifier)
15994688.0 24758 APPL ???? Notepad /Users/nicholas/Library/Application Support/VMware Fusion/Virtual Machines/Boot Camp/Boot Camp.vmwarevm/Applications/Notepad — Windows 7.app (com.vmware.proxyApp.564dbc100b31a4b5-4f8d8088bf0c3705.2009924420)
To hide it:
% appswitch -ha Notepad
To show it:
% appswitch -a Notepad
That's it.

Resources