Jump to content
kevnk

Post Notification in the middle of a bash script while loop

Recommended Posts

I have a workflow that is listening for spotify ads (so i can mute them). 

 

nWVa8QviRUSdRUAE6y0ww3zLs6sB9g.png

 

I have a while loop listening. When it finds an ad (a track with < 35 second duration), it sends a notification - but not an alfred style post notification; just the default applescript-like notification.

 

Here's the code:

 

Note: osascript -e "display notification \"Muting ad for $sleepDuration seconds\" with title \"Mute Spotify Ads\""

This is what I'd like to replace with an alfred Post Notification.


vol=`osascript -e 'tell application "Spotify" to sound volume as integer'`;

while :
	do
		state=`osascript -e 'tell application "Spotify" to player state as string'`;
		sleepDuration=1
		if [ $state = "playing" ]; then
			duration=`osascript -e 'tell application "Spotify" to duration of current track as string'`;
			duration=$(echo "$duration / 1000" | bc);

			if [ $duration -le 35 ]; then
				position=`osascript -e 'tell application "Spotify" to player position as string'`;
				position=$(echo "$position" | bc);
				sleepDuration=$(echo "$duration - $position" | bc);
				osascript -e "tell application \"Spotify\" to set sound volume to 1";
				if [ $sleepDuration -le 0 ]; then
					sleepDuration=1;
				else 
					osascript -e "display notification \"Muting ad for $sleepDuration seconds\" with title \"Mute Spotify Ads\""
					sleep $sleepDuration;
					osascript -e "tell application \"Spotify\" to set sound volume to $vol";
					sleepDuration=1;
				fi
			else 
				osascript -e "tell application \"Spotify\" to set sound volume to $vol";
				sleepDuration=1;
			fi
			sleep $sleepDuration;
		else 
			break;
		fi
	done

osascript -e "tell application \"Spotify\" to set sound volume to $vol";

Share this post


Link to post

If I read your intention correctly, you can create a new workflow trigger that does nothing but post a notification, then call that via Applescript instead of using "display notification"

 

It'll look something like 

 

tell application "Alfred 3" to run trigger "xxxxx" in workflow "xx.x.x.x.x" with argument xxxx

 

​but the workflow trigger object will give you the exact code

Edited by dfay

Share this post


Link to post

Awesome! That was helpful. It took me a while to find the "new workflow trigger" you were talking about, but it's Triggers > External and as soon as you create the id, it gives you all the code you need.

 

Thanks so much!

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...