Apple script notification - applescript

I need an applescript that sends a push notification, but when it sends one, i can find the notification only when i click on the notification center, but it doesnt "appere" in the upper right corner. Here is the code:
tell application "Finder" to activate
set inputVolume to input volume of (get volume settings)
if inputVolume = 0 then
set inputVolume to 100
display notification "unmuted" sound name "Frog"
else
set inputVolume to 0
display notification "muted"
end if
set volume input volume inputVolume
PS, The sound of the frog also doesnt work. Im happy with every advice of you!

Save your script as application, go to Notification Center settings, allow your application to send notifications (alerts). If you want to send notifications directly from the script editor, allow notifications for your script editor app instead.
I edited your script as well, because it contained errors.
set inputVolume to input volume of (get volume settings)
if inputVolume is missing value then return
if inputVolume = 0 then
set volume input volume 100
display notification "unmuted" sound name "Frog"
else
set volume input volume 0
display notification "muted"
end if

Related

How To Monitor Contents Of Folder Without Blocking Finder

As you can see from my code below I am extremely new to this. My code just about works, but my major issue is that it hogs up Finder and sometimes it does not set the Desktop picture, but does most of the time!
The script just monitors a folder, and if an "***.jpg" is added then the Desktop picture set to it.
This is my very first script so I have a lot to learn,
set reset to ""
display notification "Alarm Front Active " & (current date) as string
tell application "Finder"
set path_to_sourceFull to ":photo:FRONT CAM 1:20190929:images" -- from nsa310 network drive
set path_to_source to ":photo:FRONT CAM 1:20190929:images" -- from nsa310 network drive
set directory1 to "/Volumes/photo/FRONT CAM 1/20190929/images" as text -- from nsa310 network drive
set path_to_destinationFull to "Macintosh HD:Users:rekordbox:Documents:temp folder 2"
set path_to_destination to ":Users:rekordbox:Documents:temp folder 2"
set directory2 to "/Users/rekordbox/Documents/temp folder 2" as text
repeat while reset = ""
set allok to ""
set filelist to name of every item in folder path_to_source --of startup disk
set listSizesaved to count of filelist
delay 1
repeat while allok = ""
set filelist to name of every item in folder path_to_source --of startup disk
set listSize to count of filelist
if listSize = listSizesaved then
else
set filelist to name of every item in folder path_to_source --of startup disk
set listSize to count of filelist
set LastAddedFile to item listSize of filelist
set allok to "ALARM"
set listSizesaved to listSize -- (save the updated) count
set activefile to (path_to_source & LastAddedFile)
set selectedpicture to (directory1 & "/" & LastAddedFile)
tell application "System Events" to tell every desktop to set picture to selectedpicture
delay 1
display notification "ALARM FRONT TRIGGERED...." & (current date) as string
delay 1
end if
end repeat
end repeat
end tell
The script you want, I think, is this:
on adding folder items to thisFolder after receiving filelist
set droppedFile to first item of filelist
tell application "System Events"
tell every desktop
set picture to droppedFile
end tell
end tell
end adding folder items to
(I've left out the 'Alarm' bit, since I wasn't sure what the point of it was.)
To use this script, copy it into Script Editor, save it in the folder ~/Library/Scripts/Folder Action Scripts/, then open the applet 'Folder Actions Setup'. Add the folder you want on the left-hand side, and choose the file you just saved on the right. It should look something like this:
...where the checkmark on the left shows that folder actions are enabled for the folder (which I called 'test folder') and the script (which I called 'FADtop.scpt') is attached.
Drop an image in the folder, and it should just work.
As a general rule, don't script the Finder unless you absolutely need to; always use System Events. The Finder is a busy app, and scripting it can gum up the system. And also try to avoid this design pattern:
(* Don't do this! *)
repeat
(* test for something *)
delay x
end
The delay command is not particularly resource-efficient. If you really want to use a polling system to test for some event, it's often better to create a stand-alone app with an on idle handler. That way you let the system wake and sleep the script, with significant performance improvements.
EDIT
Since folder actions don't seem to be working with ftp drops onto remote drives, here's a reasonably efficient folder-polling approach. Save the following script as a stay-open application (choose 'Application' as the file type, and click the 'stay open' checkbox). Then launch the application and leave it running in the background.
property dateOfLastFileChosen : missing value
property targetFolder : "/Volumes/photo/FRONT CAM 1/20190929/images"
property idleTime : 300 -- 300 seconds is five minutes
on run
end run
on idle
tell application "System Events"
if exists folder targetFolder then
if dateOfLastFileChosen is missing value then
set recentFiles to every file of folder targetFolder whose visible is true
else
set recentFiles to every file of folder targetFolder whose modification date > dateOfLastFileChosen and visible is true
end if
set newFile to my mostRecentFileOfList(recentFiles)
if newFile is not missing value then
set dateOfLastFileChosen to modification date of newFile
tell every desktop
set picture to (POSIX path of newFile)
end tell
end if
end if
end tell
return idleTime -- check every 5 minutes (300 seconds)
end idle
on mostRecentFileOfList(fileList)
set maxDateObj to missing value
repeat with thisFile in fileList
if maxDateObj is missing value then
set maxDateObj to contents of thisFile
else if modification date of thisFile is greater than modification date of maxDateObj then
set maxDateObj to thisFile
end if
end repeat
return maxDateObj
end mostRecentFileOfList
Without trying to steal the thunder from #Ted Wrigley, whose solution provided the AppleScript code for the folder action, I felt there were enough comments and items for me to add to post it as another answer to the OP's dilemma.
First I will address the tell every desktop set picture to droppedFile lines of code in the following AppleScript Folder Action. If the user has only one monitor/display attached to the computer, but has created several different "Spaces", the tell every desktop set picture to droppedFile lines of code will only change the Desktop Picture for the Desktop of the current active "Space" only. The other Desktop backgrounds will not be changed. However, if the user has several monitors/displays attached to the computer, the tell every desktop set picture to droppedFile lines of code will change the Desktop Pictures for the Desktops of the current active "Space" for each attached monitor/display. If the latter is not the desired result, then tell every desktop should be changed to tell current desktop.
After testing the AppleScript Folder Action code provided by #Ted Wrigley, I noticed the image file being downloaded from an FTP server, to the test folder where I have the folder action script attached to, looked like this before the image was actually finished transferring. Because the file was kind of there and not there, it did not trigger the Folder Action.
Next, I figured I would add a delay to be beginning of the Folder Action code to allow for the transfer of the image file from the FTP server, to complete. I added a delay of 180 seconds to allow for the transfer to complete and it worked. When the transfer was complete, the file look like this.
Depending on how many files you foresee being transferred at any given time along with factoring in for file sizes... It's possible you may need to significantly increase the Delay time.
on adding folder items to thisFolder after receiving theseFiles
delay 180
set newBackground to first item of theseFiles
tell application "System Events"
set picture of current desktop to newBackground -- Single Display Attached
--set picture of every desktop to newBackground -- Multiple Displays Attached
end tell
end adding folder items to

How to activate full screen apps in background via Apple Script

I've been working on an application in Automator that changes the desktop background to the album art of the current song playing in iTunes.
You can download my first version here.
The most annoying issue I have found is that when there is a full screen app open on the same display being updated, whenever the song changes the background flickers between the current song and the previous.
This is my current code (Updated from version 1.0 above):
You may need to scroll within the code to see all of it.
tell application "System Events"
set fileName to (((path to desktop) as text) & ".iTunesArt2-1.jpg")
set oldFile to open for access file fileName with write permission
write 0 to oldFile
close access oldFile
delete file fileName
if process "iTunes" exists then
tell application "iTunes"
if (player state is not stopped) then
-- get the raw bytes of the artwork into a var
tell application "iTunes" to tell artwork 1 of current track
set srcBytes to raw data
end tell
-- write to file
set outFile to open for access file fileName with write permission
-- truncate the file
set eof outFile to 0
-- write the image bytes to the file
write srcBytes to outFile
close access outFile
end if
end tell
tell desktop 2
set picture to fileName
end tell
end if
set fileName to (((path to desktop) as text) & ".iTunesArt2-2.jpg")
set oldFile to open for access file fileName with write permission
write 0 to oldFile
close access oldFile
delete file fileName
if process "iTunes" exists then
tell application "iTunes"
if (player state is not stopped) then
-- get the raw bytes of the artwork into a var
tell application "iTunes" to tell artwork 1 of current track
set srcBytes to raw data
end tell
-- write to file
set outFile to open for access file fileName with write permission
-- truncate the file
set eof outFile to 0
-- write the image bytes to the file
write srcBytes to outFile
close access outFile
end if
end tell
tell desktop 2
set picture to fileName
end tell
end if
end tell
I got the actual code that saves the artwork to a file from here.
The desktop will not update unless you give it a new file name to update, therefore I have duplicated the process with "iTunesArt2-1" and "iTunesArt2-1".
The first 2 in '2-1' or '2-2' simply means the second desktop, as I have two different applications to change each desktop, and use my 2nd desktop for testing.
The entire application is set to loop for 1000 years, using three separate Loop functions in Automator (720 minutes, 730 times & 1000 times).
When first trying to debug this issue, the process was duplicated into four separate scripts, one for saving the image, then setting as background, and two more scripts to repeat the process with a new file name.
Here's an example of my debugging:
I remove the opening period from ".iTunesArt##.jpg" so that I can see the files on my desktop.
I play a Coldplay song and run the application in Automator to set the background.
"iTunesArt2-1.jpg" and "iTunesArt2-2.jpg" are shown on my desktop with the correct album art.
I stop the application, and play a Paramore song.
I run the first script of the application (Saves the album art).
"iTunesArt2-1.jpg" is updated to the Paramore artwork.
I run the second script of the application (Sets background image).
Note, this script should be setting the background to the Paramore image.
The background remains set to the Coldplay image.
At first I think this is only because the background image was already set to "iTunesArt2-1.jpg", and therefore the system will not try to update it again, not knowing the data in the file has been changed.
So I run the next script which should force the background to update:
I run the third script in the application.
"iTunesArt2-2.jpg" is updated to the Paramore artwork.
I run the forth script in the application.
The desktop background is updated to the Paramore artwork.
So, we can confirm that scripts 3 & 4 worked correctly.
According to the code, when the application loops back to scripts 1 & 2, the desktop background should remain as the Paramore artwork.
BUT...
I run the first script in the application.
"iTunesArt2-1.jpg" remains the Paramore artwork (as it should).
I run the second script in the application.
This script should update the desktop background from ""iTunesArt2-2.jpg" (Paramore) to "iTunesArt2-1.jpg" (Paramore).
The desktop background changes to the Coldplay artwork.
Now that makes zero sense at all.
This will happen every time I loop through the scripts.
Script 4 will change the desktop to Paramore and script 2 will change it back to Coldplay.
Remember this issue only happens when there is a full screen app open on the same display being updated. ie. A full screen app open on my primary display doesn't matter.
I have found sometimes that sliding over to the full screen app and 'activating' it will stop the flickering, and the background will be set correctly.
There surely is nothing 'wrong' with my code is there? If not, I need a way to get around this issue.
Specifically, is there a way of 'activating' every full screen app in the background without moving to those spaces?
I would love anyone who wants it to have this program, but it needs to be able to run under all circumstances.
Any help would be greatly appreciated.
Found a work around which creates a random file name each time, meaning the desktop can't get confused as to which image it is supposed to display.
screenNum refers to which screen the desktop is being set to.
set randID to screenNum
set randLoop to 0
repeat while randLoop is not 9
set randNum to (random number from 0 to 9) as text
set randID to randID & randNum
set randLoop to randLoop + 1
end repeat
Then included randID in the file name upon its creation.
Have not noticed any flickering using this method.
Note: This solves my problem, but does not answer my initial question of how one can activate a full screen app in the background.

In Applescript, Retrive playlist information from Spotify, and enable/disable shuffling

I know that there are many commands in AppleScript for Spotify such as the simple playpause command, but how would I pull a playlist's information from Spotify, and paste it in a choose from list? I would like it to take all of the songs from wherever you're listening from, and paste them in a choose from list so you can choose which song you would like to listen to. Is this even possible? Can I do something similar?
Also, how would you enable/disable shuffling?
Additionally, is there a way to search Spotify through AppleScript?
I'm not sure if any of these are possible, and Google doesn't have any info on this right now. Does anyone know how to do this?
The Spotify AppleScript implementation does not have specific commands to get at playlist information -- it only exposes "current track" to get info about the currently-playing track and "next track" to play the next track. You can work around this limitation to build an AppleScript array containing all the tracks in a current playlist.
set trackNameList to {}
set trackIDList to {}
tell application "Spotify"
activate
set shuffling to false
set repeating to true
set sound volume to 0
if player state is not playing then
playpause
end if
set trackID to spotify url of current track
repeat while trackIDList does not contain trackID
set end of trackIDList to trackID
set end of trackNameList to name of current track
next track
delay 1 -- otherwise Spotify misbehaves
set trackID to spotify url of current track
end repeat
end tell
tell me to activate
set chosenNames to choose from list trackNameList without multiple selections allowed
set chosenName to (item 1 of chosenNames) as string
repeat with i from 1 to count of trackNameList
set itsName to (item i of trackNameList) as string
if itsName is chosenName then
exit repeat
end if
end repeat
set trackID to (item i of trackIDList) as string
tell application "Spotify"
activate
set sound volume to 100
play track trackID
end tell
There is no AppleScript support for searching in Spotify.

AppleScript Prompt User for Clicks

For my studies I use a lot the screencapture feature as a terminal command. Recently I started to use AppleScript to automate some of those screen captures. But now I would like to push the thing a bit further.
Is it possible to use a command such as "display dialog", but to query 2 clicks, of which the coordinates would be assigned to the screencapture command so that it takes the picture of the screen?
I was thinking of something like this:
set click1 to "0,0"
set click2 to "0,0"
display dialog "Click where the screen area to grab begins" & click1
display dialog "Click where the screen area to grab begins" & click2
do shell script "screencapture -R click1,click2 /Users/user/Desktop/name_of_the_file.png"
Applescript does not have any mechanism for capturing mouse clicks, or even getting the position. You can get the mouse position using a call to the framework.
But, you will have to resort to a cocoa app solution to capture screen clicks.
One idea is you could use an applescript flow that gives you 2 seconds to place the first mouse position, then it can beep to tell you it has captured it, and then you have two more seconds to place the pointer in the second position before it captures it.
Something like this:
use framework "Foundation"
use scripting additions
display dialog "Place your mouse pointer in the first position within 2 seconds, then after the beep, place it in the second position within 2 seconds."
delay 2
set theList to current application's NSEvent's mouseLocation()
set xCoord1 to theList's x
set yCoord1 to theList's y
beep
delay 2
set theList to current application's NSEvent's mouseLocation()
set xCoord2 to theList's x
set yCoord2 to theList's y
beep
display dialog (((xCoord1 as string) & ", " & yCoord1 as string) & return & xCoord2 as string) & ", " & yCoord2 as string

How do i check if there is an Airplay device connected

I'm wanting to do a script that needs to check if there is an airplay device connected and being used.
I have tried searching in many places but i can't seem to find any documentation on this, does anyone have any idea how i would do this?
To further clarify, I want a script that is something along the lines of:
if airplay device is in use then //(pause for longer)
else //(the script that is already there)
this is the current code i have
global okflag
set okflag to false
set front_app to (path to frontmost application as Unicode text)
-- check if iTunes is running
tell application "System Events"
if process "iTunes" exists then
set okflag to true --iTunes is running
end if
end tell
if okflag is true then
set CV to output volume of (get volume settings)
tell application "iTunes"
if (player state is playing) then
set currentVolume to the sound volume
set Timer to ""
display dialog "Sleep Time (Minutes)" with title "iTunes Timer" default answer Timer buttons {"Cancel", "OK"} default button 2
set Timer to text returned of the result
set the_button to button returned of (display dialog "Shutdown after countdown?" with title "iTunes Timer" buttons {"No", "Yes"} default button 2)
delay Timer * 60
repeat with i from currentVolume to 0 by -1
set the sound volume to i
delay 0.6
end repeat
pause
set player position to (player position - 60)
--Restore original volume
set the sound volume to currentVolume
if the_button is "Yes" then tell application "System Events"
shut down
end tell
else
display dialog "Nothing Playing" with title "iTunes Timer" giving up after 2
end if
end tell
else
display dialog "iTunes is not running" with title "iTunes Timer" giving up after 2
end if
edit: added application for clarification of what i needed to find out
Hmm.
But did not workout how to get a status if you are streaming say from iTunes. (so far)
But did workout that if airplay is being used as a Extended desktop space or Mirroring then That is picked up in the System Profile for displays. SPDisplaysDataType
This applescript accounts for that at least.
set connected to ""
try
set connected to do shell script "system_profiler SPDisplaysDataType | grep -i \"AirPlay\" "
if connected contains "Connection Type: AirPlay" then
set connected to "Connected as a Display or mirroring"
end if
on error err
log err
if err contains "The command exited with a non-zero status" then
--do somthing
set connected to "Not connected as a Display or mirroring"
end if
end try
connected
UPDATE*
The iTunes commands now have some airplay commands. Not sure when they appeared.
But one example of use:
tell application "iTunes"
set isPlaying to player state
if isPlaying is playing then
log isPlaying
set airPlayEnabled to AirPlay enabled
if airPlayEnabled then
log airPlayEnabled
else
airPlayEnabled
end if
else
log isPlaying
end if
end tell
The AirPlay enabled AFAIK only for the iTunes player it is run against. And not for any other device that may be occupying the Airplay device

Resources