AppleScript plays playlist in Music.app on macOS Monterey - applescript

I'm working on this Alfred Workflow: https://github.com/gustavosaez/Alfred-Workflow-Apple-Music-Playlist
And today I'm looking for a day to "automate" the play music in background or hidden (to avoid open the application and click PLAY).
I found a way to set the mouse click on a screen position (specific on the play button), but it works only if the app is visible and if the resolution of screen is the same of mine.
I search about UI Elements for Music.app and didn't find anything.
The problem is:
When Alfred opens the chosen link, Music.app take a few seconds to open and load the playlist selected;
After this, space/play command is inactive, so I think to create a script that gets the current page and play the playlist in shuffle mode.
Any idea?
some ideas:
tell application "Music"
-- tell application "System Events" to tell process "Music" to click at {620, 374}
play current playlist
end tell
|
tell application "Music"
-- tell application "System Events" to tell process "Music" to click at {620, 374}
play {workflowPlaylistName}
end tell
tell application "System Events"
keystroke "h" using command down
end tell

I'm confused why you are trying to use mouse clicks or keystrokes, instead of just using
tell application "Music"
play
end tell
or
tell application "Music"
play the playlist named "{query}"
end tell
and pass the name of the playlist as query

Related

Activate the previous app as soon as new app opens

I want Applescript that trigger as soon as Mail app opens and if mail app opens then it should wait for 10 seconds and after that it should activate the previous app (like what cmd + tab does).
I have used the following code but in order to achieve that, it should run in the background all the time. I have no clue how to achieve this.
tell application "System Events"
set frontmostApplicationName to name of 1st process whose frontmost is true
end tell
tell application "Mail"
activate
end tell
tell application frontmostApplicationName
activate
end tell

How to close / cancel a pop-up modal / dialog window in Applescript

I'm trying to fix a particular problem in this Applescript I made: https://gist.github.com/jwmann/08daed8a905cfbf4ff96
Context:
It's an Applescript where you select a song in a VLC playlist, run the Applescript and it will trash the song from the original location.
The Problem:
The problem occurs when a user attempts to delete a currently playing, single song where the VLC playlist is the size of just the single song.
Now VLC will explode if you try to delete a song that it's currently playing. To workaround this I've made the script stop VLC from playing and trying again to delete it.
Now if this workaround happens, VLC will no longer be playing. In a multi-song playlist, this can't be annoying. So the script will continue playing VLC at the end of the script. However, after deleting the song from a single song playlist, it will remove the song from the playlist, leaving no songs and therefore no playlist.
So when the script tries to play, it'll open a new window / dialog / modal to allow the user to find something to play. This is something I don't want to happen.
What I'm trying to do:
I need a way to:
Detect the correct Window
Tell that window to Close
Information I've gathered:
This is the window I'm trying to Cancel
This is the data that Accessibility Inspector shows me about the window.
<AXApplication: “VLC”>
<AXWindow: “Open Source”>
Attributes:
AXFocused: “0”
AXFullScreen: “0”
AXTitle: “Open Source”
AXPosition (W): “x=993 y=276”
AXGrowArea: “(null)”
AXMinimizeButton: “(null)”
AXDocument: “(null)”
AXSections (W): “<array of size 1>”
AXCloseButton: “(null)”
AXMain: “0”
AXFullScreenButton: “(null)”
AXProxy: “(null)”
AXDefaultButton: “<AXButton: “Open”>”
AXMinimized: “0”
AXChildren: “<array of size 8>”
AXRole: “AXWindow”
AXParent: “<AXApplication: “VLC”>”
AXTitleUIElement: “<AXStaticText>”
AXCancelButton: “<AXButton: “Cancel”>”
AXModal: “1”
AXSubrole: “AXDialog”
AXZoomButton: “(null)”
AXRoleDescription: “dialog”
AXSize: “w=574 h=402”
AXToolbarButton: “(null)”
AXFrame: “x=993 y=276 w=574 h=402”
AXIdentifier: “_NS:40”
Actions:
AXRaise - raise
Things I've tried:
tell application "System Events"
close window "Open Source" of application "VLC"
end tell
tell application "System Events"
click button "Cancel" of window "Open Source" of application "VLC"
end tell
tell application "System Events"
cancel window "Open Source" of application "VLC"
end tell
tell application "System Events" to tell (window 1 of application "VLC" whose subrole is "AXDialog") to close
In every example, System Events or VLC (I tried both) can't seem to find the window "Open Source" even though it's clearly called "Open Source" in the inspector. It's not a sheet, it's a window. I don't understand why I can't find this window.
Thanks to #l'L'l for recommending me Script Debugger 5, it allowed to view the data of the windows in a readable way and even provide code on calling specific elements.
How I found out was I ran this code:
tell application "VLC" to activate
tell application "System Events"
tell process "VLC"
set myUI to every UI element
end tell
end tell
The reason being is that even though I knew the window's name, Applescript couldn't ever find it, so I needed a way to see everything.
Within that, I found this:
If you look at the metadata of this window, you'll see that it's SUPER broken.
No wonder Applescript could never find it, it's barely a window.
From that list, it provides the exact code to reference parts of the window, including, the "Cancel" button.
This is the code that cancels my window:
tell application "System Events"
tell its application process "VLC"
tell its window "Open Source"
click button "Cancel"
end tell
end tell
end tell
So searching every UI Element was definitely useful and Script Debugger 5 definitely helped.

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.

Quicktime Player recording with Applescript broken on Mavericks?

I have this simple code:
tell application "QuickTime Player"
activate
new screen recording
document "Screen Recording" start
delay 10
document "Screen Recording" stop
end tell
This records a 10-second movie on my 10.8 machine with no problem.
But on 10.9 mac-mini, QT hangs at the stop action above.
It hangs at the window with message "Finishing Recording".
I have to force quit it and still same thing. If I do these steps manually, they work. But with AppleScript or even same steps with Automator have same issue.
I upgraded to 10.9.2, but still same issue.
Is this a known bug? Any suggestions for a work around?
Thanks.
I do something like this to record the screen from CI and it always works for me:
set filePath to (path to desktop as string) & "ScreenRecordingFile.mov"
tell application "System Events" to tell process "Simulator"
set frontmost to true
end tell
tell application "QuickTime Player"
activate
set newScreenRecording to new screen recording
delay 1
tell application "System Events"
tell process "QuickTime Player"
set frontmost to true
key code 49
end tell
end tell
tell newScreenRecording
start
delay 15
stop
end tell
export document 1 in (file filePath) using settings preset "720p"
close document 1 saving no
end tell
I've ran into the same problem and found out, that the problem is not in the stop, but in the non-interactive start, so I've tried to start recording (pseudo)interactively and it worked out perfectly.
tell application "QuickTime Player"
activate
new screen recording
activate
tell application "System Events" to tell process "QuickTime Player"
key code 49
end tell
document "Screen Recording" start
delay 10
document "Screen Recording" stop
end tell
It still calls start instead of emulating click, because I haven't found a way to make it work.
Two tricks are needed:
Send a space key to the QuickTime Player before start to avoid the Finishing... dialog being stuck. (credits to #outring)
We need to simulate a mouse click. This is not easy, and we need to do it via CoreGraphics (aka Quartz). We can either write in Objective-C (need compilation), in Python (via PyObjc bridge) or Swift.
Somehow recording starts roughly 2 seconds after the mouse click, so we need to sleep 12 seconds for a 10-second video.
A working script will look like this:
tell application "QuickTime Player"
activate
new screen recording
tell application "System Events" to tell process "QuickTime Player" to key code 49
document "Screen Recording" start
do shell script "python -c 'import Quartz; mouse_location = Quartz.NSEvent.mouseLocation(); [ Quartz.CGEventPost(Quartz.kCGHIDEventTap, Quartz.CGEventCreateMouseEvent(None, mouse_action, mouse_location, Quartz.kCGMouseButtonLeft)) for mouse_action in [ Quartz.kCGEventLeftMouseDown, Quartz.kCGEventLeftMouseUp ] ]'"
delay 12
document "Screen Recording" stop
end tell

Play iTunes Continously

I have an applescript that plays a specific iTunes playlist.
tell application "iTunes"
play user playlist myPlaylist
end tell
But this will only play the play list once, then it stops. What I want is for the playlist to reload, then play continuously 24/7 until someone actually stop or pause the iTunes.
I tried to modify the code to this, but it doesn't work.
on idle
tell application "iTunes"
play user playlist myPlaylist
end tell
end idle
The on idle code actually does nothing, even after iTunes stops.
I also try to reload the user playlist, but also doesn't work.
tell application "iTunes"
repeat until player state is stopped
play user playlist myPlaylist
end repeat
end tell
Thank you.
If you look in iTune's applescript dictionary at the "playlist" class you will see it has a command called "song repeat". Maybe that will help you.
tell application "iTunes"
tell user playlist myPlaylist
set song repeat to all
play
end tell
end tell
EDIT: As of itunes version 11 the above code no longer works. See this question for solutions. I posted an answer there.
The code for repeating playlist does not work on iTunes 11. The link regulusy6633 posted contains the answer. The code below works for iTunes 11.
tell application "System Events" to tell process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Repeat"'s menu 1
perform action "AXPress" of menu item "All"
end tell

Resources