App behavior via script different when display is asleep - macos

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.

Related

Applescript or Automator behaves differently at night?

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

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.

How to keep AppleScript running as long as a specific application is running?

Normally in AppleScript it goes line by line and at the end it quits the script. Is there a way to make the script keep on running? This is the situation:
Via the AppleScript I am opening Google Chrome. This is fine. After opening it the AppleScript have to keep on running until Google Chrome is closed. When closed it needs to show a dialog that Google Chrome is closed and quit itself.
Is there a way to do this?
You can also try an on idle handler...
repeat until application "Google Chrome" is not running
-- insert your code
say "run"
delay 2
end repeat
say "quit"

iMacs for public display locked kiosk application when in use and video loop when idle

I have been tasked with deploying several iMacs for public display.
They will need to have an application running at all times (Serato DJ) and upon idle they should display a looped video - using saveHollywood screensaver to loop the and play the multiple video files
I have been toying with applescript for this job - I would have script run on boot.
I think it should look something like this
Applescript -
tell application "Serato DJ"to activate
on idle
tell application "Serato DJ" to quit
tell application "System Events" to start current screen saver
end idle
I have to quit the application because it wont allow the screensaver to kick in - much like vlc
I know this isnt a lot to go on.
Can someone please help point me in the right direction
Any assistance would be greatly appreciated
Daniel
Well, I'm not sure if this question is still relevant to you, but if it is, this should work:
repeat
--This variable is how long the script waits before it turns the screen saver on
set timeToSleep to 300
--
set idleTime to do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'"
considering numeric strings
if idleTime is greater than timeToSleep then
tell application "Serato DJ" to quit
tell application "System Events" to start current screen saver
end if
if idleTime is less than timeToSleep then
if application "Serato DJ" is not running then
try
do shell script "killall ScreenSaverEngine"
tell application "Serato DJ" to activate
end try
end if
end if
end considering
delay 1
end repeat

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

Resources