Can AppleScript determine the default mail app? - applescript

I'd like to know if there's a way to use AppleScript to determine what the current default email app is. Ideally it would return the path to the program, such as /Applications/Mail.app or /Applications/Outlook.app.

You can identify the default email client from the launch services preference file.
In El Capitan the file is located in ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist. In system versions prior to 10.11 the file might be directly in the Preferences folder.
set bundleIdentifier to missing value
set LSPrefFilePath to (path to preferences folder as text) & "com.apple.LaunchServices:com.apple.launchservices.secure.plist"
tell application "System Events"
set LSPrefFile to property list file LSPrefFilePath
tell property list item 1 of contents of LSPrefFile
repeat with anItem in (get property list items)
if exists property list item "LSHandlerURLScheme" of anItem then
if value of property list item "LSHandlerURLScheme" of anItem is "mailto" then
set bundleIdentifier to value of property list item "LSHandlerRoleAll" of anItem
exit repeat
end if
end if
end repeat
end tell
end tell
if bundleIdentifier is missing value then
set defaultMailClient to "/Applications/Mail.app"
else
tell application "Finder" to set defaultMailClient to POSIX path of (application file id bundleIdentifier as text)
end if

Related

Apple Script: how do I watch for new download file with specific name to trigger a script?

I have a script that downloads a report from an online service, waits a specified amount of time e.g. 30secs for the file to actually download and then renames the file. The script then repeats
What I would like to do is instead of putting a static delay in the script, to create a trigger that looks for the new file and once it appears triggers the renaming portion of the script.
The downloaded file name is constant.
The current portion of the script looks like this
`
tell application "Finder" to activate
tell application "System Events"
delay Wait_Time
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
ultimately what I would like is to replace the delay Wait_Time with a something like:
repeat until file name found
Search Folder for "file name"
end repeat
select new file
rename new file to clipboard
obviously this wont work but it kind of captures my logic.
I have also tried a few other possible solutions but Im new to AppleScript and not overly confident that these approaches are suitable for my application.
The below is an example of a solution I have tried. This is just an example and im not sure if this particular code can even get me to where I need to be. I thought I would include it to show just how lost I am.
set excludes to {"Folder", "Application"}
tell application "Finder"
set search_folder to folder "Macintosh HD:Users:XXXXXXXX:Library:CloudStorage:OneDrive-Flinders:Flinders Connect Stats:Source Stats:Cisco Finesse:Agent State Summary by Interval Report"
set foundItems to (every item in search_folder whose name contains "Agent State Summary by Interval Report" and kind is not in excludes) as alias list
if foundItems is {} then return
repeat with once from 1 to 100
try
if (count of foundItems) = 1 then
tell application "Finder" to activate
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
exit repeat
end if
end try
end repeat
return
end tell
Im sure you can see I have no idea what im doing.
Anyway any help would be awesome!
I have found a solution... if anyone has ideas for improvement please let me know.
tell application "Finder" to activate
tell application "System Events"
repeat until (exists (files of folder folderPath whose name contains "Agent State Summary by Interval Report"))
delay 1
end repeat
end tell
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
end repeat
say "Finished!"
display dialog "Completed"
The original solution was found here AppleScript: How to repeat a search for a file until it is found?

How can I list the available AirPlay output devices on macOS?

I'm trying to retrieve a list of available AirPlay output audio devices, I don't have a preference on which language to use.
Before Ventura I used the following Apple Script:
set devices to {}
tell application "System Preferences"
reveal pane id "com.apple.preference.sound"
end tell
tell application "System Events"
tell application process "System Preferences"
repeat until exists tab group 1 of window "Sound"
end repeat
tell tab group 1 of window "Sound"
click radio button "Output"
tell table 1 of scroll area 1
set selected_row to (first UI element whose selected is true)
set currentOutput to value of text field 1 of selected_row as text
repeat with r in rows
try
set deviceName to value of text field 1 of r as text
set deviceType to value of text field 2 of r as text
set end of devices to { deviceName, deviceType }
end try
end repeat
end tell
end tell
end tell
end tell
if application "System Preferences" is running then
tell application "System Preferences" to quit
end if
return [ devices, "currentOutput", currentOutput ]
But since the Ventura update this broken and it seems like there's no way to adapt it to work with Apple Script anymore.
Does anyone know how to update it to work with Ventura or could point me to the documentation of either Swift or ObjC to retrieve this list?
You can get system information for audio devices in the XML form, then parse it someway for audio devices names. I don't think following parsing is very good, but it works for me at least:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set theXML to do shell script "system_profiler SPAudioDataType -xml"
set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)
set {theMatches, theError} to (theXMLDoc's nodesForXPath:("//array") |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set temp to (theMatches's firstObject()'s valueForKey:"stringValue") as text
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_name", "coreaudio", "_properties"}
set tempItems to text items of temp
set AppleScript's text item delimiters to ATID
set audioDevicesNames to {}
repeat with anItem in tempItems
set anItem to contents of anItem
if anItem is "" or anItem begins with "_" then
-- do nothing
else
set end of audioDevicesNames to anItem
end if
end repeat
audioDevicesNames

How do get last downloaded image to insert into object placeholder of current keynote slide?

Images are key to my work.
I am trying to find a quick way to automate image insertion to keynote. The script I am drafting is to get the last created/downloaded image, and insert it into the current slide on Keynote as a replacement of the master's object placeholder.
Error from applescript:
"Keynote got an error: Can’t set file name of slide 2 of document id \"B1054797-9A07-4642-AE79-166D1DE72674\" to alias." number -10006 from file name of slide 2 of document id "B1054797-9A07-4642-AE79-166D1DE72674" to alias
set myFolder to "/Users/Mingyu/Desktop/UpperEchelon"
tell application "Finder" to set latestFile to item 1 of (sort files of (POSIX file myFolder as alias) by creation date) as alias
tell application "Keynote"
activate
tell the front document
tell the current slide
set thisPlaceholderImageItem to item 1
set file name of thisPlaceholderImageItem to ¬
alias
end tell
end tell
end tell
I expect the script will insert last download image in the folder "UpperEchelon" to a current open slide in Keynote
Two issues:
You have to set thisPlaceholderImageItem to image 1 rather than to item 1
You have to set the file name to latestFile rather than just to alias. That's what the error is telling you.
And if the folder is a subfolder of the desktop there is a much shorter syntax
set myFolder to "UpperEchelon"
tell application "Finder" to set latestFile to item 1 of (sort files of folder myFolder by creation date) as alias
tell application "Keynote"
activate
tell the front document
tell the current slide
set thisPlaceholderImageItem to image 1
set file name of thisPlaceholderImageItem to latestFile
end tell
end tell
end tell

Select the first image in a folder selected in the Fiinder

I have a script that changes the icon of a folder with the image you select and it works fine but I want to be able to have the script automatically select the first image of the folder and assign that as the image that will be used to create the icon.
This is what I have so far. It is unable to select the image and gives various error messages depending on what I try. Any help would be greatly appreciated!
set exePath to (path to home folder as text) & "downloads:SetFileIcon"
tell application "Finder"
set theSelection to selection -- This is a list of selected items, even if only 1 item is selected.
set theFile to (item 1 of theSelection) as text -- Get the first item in the list. We make it text so we can convert it to a posix path for SetFileIcon.
set theimage to item 1 of theSelection
set imagePath to (path to desktop folder as text) and theimage
end tell
do shell script quoted form of POSIX path of exePath & " -image " & quoted form of POSIX path of imagePath & " -file " & quoted form of POSIX path of theFile
A couple of issues with your script at first glance. First, you’re never asking for the contents of the folder. You set theFile to item 1 of theSelection and then you set theimage to item 1 of theSelection. I expect you meant to set theimage to item 1 of the document files of theFile. Note that theFile is actually the selected folder.
Second, “and” is a boolean operation; to concatenate text in AppleScript, use “ & ”. Thus, even if theimage contained the image’s name, you would need to use & theimage.
However, since you are asking the Finder for the information, the Finder already knows the full path to any particular file. So, once you get an image, you can ask the Finder for the path directly.
tell application "Finder"
-- This is a list of selected items, even if only 1 item is selected.
set theSelections to the selection
-- This code assumes that the first selected item is a folder
set theFolder to the first item of theSelections
-- we want the first document that is also an image
set theImages to the document files of theFolder whose kind contains "image"
set theImage to item 1 of theImages as text
-- and we want it's path
set imagePath to POSIX path of theImage
end tell

Applescript Traversing Directory

I have a script that add or update files in a folder. What I want to do is for an applescript to add these files to iTunes. But I can't seem to loop through the directory.
tell application "iTunes"
if playlist myPlaylist exists then delete playlist myPlaylist
set newPlaylist to (make new user playlist with properties {name:myPlaylist})
repeat with theFile in myPath as list
add theFile to playlist myPlaylist
end repeat
end tell
For every repeat, the value of theFile is the individual character of myPath, not the actual file in myPath. theFile will be M, a, c, i, n, ... instead of file1.mov, file2.mov, ...
Thank you.
If myPath is a string, as list converts it to an array of characters.
You can tell Finder to get a list of aliases:
tell application "Finder"
set l to items of (POSIX file "/Users/username/Music/folder" as alias) as alias list
end tell
tell application "iTunes"
if playlist "test" exists then
delete tracks of playlist "test"
else
set p to make new user playlist with properties {name:"test"}
end if
add l to p
end tell
This would add the selected files to the currently shown playlist:
tell application "Finder"
set l to selection as alias list
end tell
tell application "iTunes"
add l to view of browser window 1
end tell

Resources