You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Simple Applescript for toggling Play/Pause in Firefox?

First off, while streaming a radio station's web site the F8 key works as it should when I tap it.  Trying to add clock radio function in my Macbook Pro running Catalina.  Reck'n Automator can set a time to run an Applescript like the below, if I can get past it.  With it I get no action and the system alert sound, but it does compile.

tell application "System Events"
  key code 100
end tell

I have AppleScript and Firefox allowed in Accessibility in the Security & Privacy preference panel.  I have abandoned apps like Tune-In, for they now arbitrarily insert commercials into songs.  I only stream directly from a station's web site now, so this script must work in FireFox.


Dave


MacBook Pro 13″, macOS 10.15

Posted on May 13, 2023 12:43 PM

Reply
Question marked as Top-ranking reply

Posted on May 18, 2023 1:55 PM

If muting is your goal now, that's even easier, since the OS handles that without having to interact with any given application.


You can simply mute the volume via the one-line command:


set volume with output muted


you can reverse this via:


set volume without output muted



You can also set a toggle via something like:


set isMuted to output muted of (get volume settings)
if (isMuted) then
	set volume without output muted
else
	set volume with output muted
end if


which gets the current muted state and inverts it.

5 replies
Question marked as Top-ranking reply

May 18, 2023 1:55 PM in response to InsertPithyUsernameHere

If muting is your goal now, that's even easier, since the OS handles that without having to interact with any given application.


You can simply mute the volume via the one-line command:


set volume with output muted


you can reverse this via:


set volume without output muted



You can also set a toggle via something like:


set isMuted to output muted of (get volume settings)
if (isMuted) then
	set volume without output muted
else
	set volume with output muted
end if


which gets the current muted state and inverts it.

May 18, 2023 10:05 AM in response to InsertPithyUsernameHere

Can you refine your question. it isn't clear what you're trying to do, so I'm kinda stabbing in the dark here.


It sounds like you're trying to simulate a press of the F8 key in Firefox to toggle some web app... fair enough.


However, nothing in your script targets Firefox - the script just blindly presses the key, and there's nothing to say that Firefox is the frontmost app and therefore will receive the keystroke.


Try forcing Firefox to be frontmost before invoking the F8:


tell process "Firefox" to activate
tell application "System Events"
    key code 100
end tell

May 18, 2023 12:12 PM in response to Camelot

Thanks-

I think you meant application Firefox, right? Calling it a process didn't let it compile. Application did, but no results. Actually I now think toggling the mute key would be better. Streaming often speeds up in a jerky fashion until it catches up when you pause it then resume. Has happened on several of my devices now.


Anything else to try?

May 18, 2023 6:32 PM in response to Camelot

SWEEEET! Works like a charm. TY so much. Now, I need a scheduled trigger to make my Macbook function essentially as a clock radio-the reason I posted. Checking Calendar, it's simple to launch the script every day, but I only need it on weekdays, for work. I'd have to schedule an event to trigger in iCal for every weekday. There must be a more practical and less tiring way.


I just tried Task till Dawn, where scheduling is flexible to have it execute on any or every day like an alarm clock. Trouble is, I can't select my script, or any other file! The button isn't there like I saw in an online tutorial. Very strange. I wrote the author to find out why. So close though...

Simple Applescript for toggling Play/Pause in Firefox?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.