Applescript or Automator behaves differently at night? - macos

I have a simple Automator app to launch an app at midnight, and then am automator worksflow to quit at 6am. Both are run through the calendar. Launching works fine. I'm having problems quitting, but only at night.
If I run the quitting script now, it works fine. If I run the script via setting the calendar to a few minutes from now, it works fine. But when set for 6am, I check in the morning, its not worked.
My system is a Mac Mini macOS Catalina. The app is Bittorent Web. Bittorrent Web launches a small app that then launches Safari. So for my 'quit' workflow, I first quit Safari using an Automator function. Next I use a script to quit Bittorrent because Bittorrent asks if you're sure you want to quit. The script brings bittorent to the front, runs command-q, then hits return. This part doesn't work at night.
The machine is set to not sleep. The automator and the automator app has full permissions to run scripts - as I say if I were to run it now it would work. So i'm lost. The script is;
on run {input, parameters}
tell application "System Events"
set frontmost of process "BitTorrent Web" to true
keystroke "q" using command down
keystroke return
end tell
end run
Hope this makes sense. As I said, this works now, but in the mornings I find that Safari is closed, but BitTorrent is still running - no errors, and the "do you want really want to quit" dialog box is not their either. Thanks

A better and more reliable approach is
on run {input, parameters}
if application "BitTorrent Web" is running then quit application "BitTorrent Web"
end run
Alternatively address the process explicitly (the process name might be different)
on run {input, parameters}
activate application “BitTorrent Web”
tell application "System Events"
tell process “BitTorrent Web”
keystroke "q" using command down
keystroke return
end tell
end tell
end run

Related

App behavior via script different when display is asleep

I have an AppleScript that runs every morning and evening. It tells Quicken (Mac) to update all accounts.
I have to do this because Fidelity has some private funds and the only way to get daily stock prices for them is to actually update the account.
My script runs perfectly via a shell script that is executed by launchd, provided that the display is not asleep. When the display is asleep, the script runs without error, but Quicken does not update.
Note: My Mac is set to prevent the computer from sleeping when the display is asleep.
AppleScript:
tell application "Quicken"
reopen
activate
end tell
tell application "System Events"
keystroke "U" using {command down, shift down}
delay 1
set visible of application process "Quicken" to false
end tell
Command in .sh script:
/usr/bin/osascript /xxx/bin/Stocks/scripts/QuickenUpdate.scpt 2>&1
Anyone know why it won't update when the display is asleep ?
Possible other causes for the behavior ?
Thanks.

Opening a new Safari window with Alfred AppleScript opens 2 windows

When I run this script from Alfred (using the text "nsafari", and Safari is quit (not in the dock), two Safari windows will pop up. When I run it from the Script Editor, it will sometimes open up two windows, but sometimes not. (This also happens with my new Safari window script as well).
if application "Safari" is running then
tell application "Safari"
make new document
activate
end tell
else
tell application "Safari" to activate
end if
Why does it open two windows only from Afred? And how do I make it only create one?
I don't know why, but after two years, the problem I reported above does not seem to happen any more...
(and yes this is not a very satisfying answer)

AppleScript works in Script Editor but not as application

I am pretty new to programming, especially with AppleScript. I wrote a simple script for Valentine's Day to play a song from iTunes and then open a flash animation file in Safari. When I run the script in ScriptEditor, everything works as desired, but when I export as a standalone application, it fails at the command to enable full-screen mode. I am assuming it is an issue with System Events. To be clear, the application functions to the end, but at the keystroke command I hear an alert sound and the window remains as-is.
I am running Yosemite, and am fully updated.
Ideally, I would like to open the file in Google Chrome to utilize Presentation Mode, but I can't even get Chrome to open the file.
Thanks for any advice! Here is the code:
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
close every window
end tell
set volume output volume 75
tell application "iTunes"
set currentVolume to sound volume
if player state is playing then
stop
back track
end if
play track "The Promise"
set player position to 6
end tell
delay 4
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
end tell
I agree with Jerry Stratton's comment that it could be an accessibility issue. However it also could be that you are issuing the keystroke command before Safari is ready to accept it. If it's opening a file then it could be busy and miss the keystroke command.
Also, I would move the system events code outside the Safari code and also just tell system events, rather than the Safari process, to perform the keystroke command. Try this as the Safari and System Events parts.
NOTE: I can't get Chrome to open a file either.
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
end tell
tell application "Safari" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
Most likely you’ll need to allow your standalone application to use System Events. At some point you needed to do that for Script Editor; you’ll need to do the same for your standalone app.
You’ll find the option in System Preferences under Security & Privacy, then Privacy, and then Accessibility. There’ll be a list of apps, and your app is probably listed there without a check for “Allow the apps below to control your computer.”
You may need to use the “+” button to add your app to the list.
I have verified that I can use this simple script to make Safari full-screen; it will work if the app is given permission under Accessibility, and it will silently fail if not.
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
This is Yosemite, Mac OS X 10.10; it may be different in other versions of Mac OS X.

In applescript, is there any way to specify which desktop a file is opened in?

When my system boots I would like to run applescript that opens files in three different desktops/spaces.
First Space: Mail and Things (my to do list program)
Second Space: Textmate and a Safari for my first project
Third Space: Textmate and a Safari for my second project
First, in Mission Control I created two more desktops which will remain there the next time my system boots unless they are manually removed. Instead of creating one long script, I chained three applescripts (boot1, boot2 and boot3) to break it up into simpler blocks of code. At the end of boot1 you will see:
run script file "<drive name>:Users:<username>:boot2.scpt"
In boot2 and boot3 you will see a bunch of delay lines. One thing I dislike about applescript is that it often starts processing the next command before the OS finishes responding to the prior one. This causes inconsistencies and errors. Delays are a hack to force things to slow down. They help, but even when you use them things are still a bit dicey. In boot2.script:
# this emulates the keyboard shortcut to move to desktop 2
# there doesn't seem to be any way to modify an `open` command to open a file on desktop 2
tell application "System Events"
delay 2
# key code 19 is the key code for the number 2.
# <cntl> 2 is the shortcut to get to desktop 2
key code 19 using control down
end tell
tell application "TextMate"
activate
# 'sites' is the name of the directory my projects are in
open "/users/<username>/sites/project1/"
end tell
tell application "Terminal"
activate
do script "cd /users/<username>/sites/project1/"
delay 2
do script "rails s" in front window
delay 2
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
tell application "System Events" to tell process "Terminal" to keystroke return
delay 2
do shell script "open -a Safari http://localhost:3000"
end tell
OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough. Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop. This is the next problem. How do I overcome that?
2305491 is no longer relevant because space preferences are gone.
Thanks.
Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop.
There is an option in the Mission Control Preferences called "When switching to an application, switch to a Space with open windows for the application". Uncheck this.
OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough.
Better solution is always something like this
repeat until something exists
delay 0.1
end repeat

How to check if application is open via Automator in MAC OS?

Im trying to check if an application is open via an AppleScript
This is the following code :
on run {input, parameters}
quit application "KeyboardViewer"
return input
end run
However I want it to check if KeyboardViewer is open in the first place, if it is then quit application, if not, then launch it. In a way, the code should toggle the application.
I am not familiar with coding for AppleScript (first timer here) so I would appreciate some insight.
Thanks
edit
I have been trying to work with it and this seems to work, is there a more efficient way ? I would like to see your inputs please
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
activate application "KeyboardViewer"
end if
return input
end run
There is a small (less than 1s) delay when using this script. Is there a way to make it faster ?
This seems to work :
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
activate application "KeyboardViewer"
end if
return input
end run
What I learned is that if application "APPLICATION NAME" is running checks if the application is open.

Resources