I am trying to clean the holes out of my Mac address book. As a first step I want to ask all my friends for their birthday, to be able to congratulate them with cheesy Hallmark cards.
I need a "group" in my address book, to mailmerge personalized messages from.
This is the Applescript I came up with:
tell application "Address Book"
make new group with properties {name:"No Birthday"}
set birthdayPeople to (get every person whose birth date is greater than date "Monday, January 1, 1900 12:00:00 AM")
repeat with i from 1 to number of items in people
set thePerson to item i of people
if not (birthdayPeople contains thePerson) then
add thePerson to group "No Birthday"
end if
end repeat
save
end tell
It breaks, but from the error messages I cannot deduce what is wrong:
Result: error "Can’t make «class azf4»
id
\"05F770BA-7492-436B-9B58-E24F494702F8:ABPerson\"
of application \"Address Book\" into
type vector." number -1700 from «class
azf4» id
"05F770BA-7492-436B-9B58-E24F494702F8:ABPerson" to vector
(BTW: Did I mention this is my first AppleScript code, EVER? So, if this code can be simplified, or made more elegant, that is welcome too.)
We can simplify this down to five lines:
tell application "Address Book"
add (every person whose birth date is missing value) to ¬
make new group with properties {name:"No Birthday"}
save addressbook
end tell
It turns out that add can take a list as well as a single object, so we can skip the loop. (Note that if you had used a loop, you could have used the repeat with thePerson in people ... end repeat form, as that's cleaner than what you had.) Next, it appears that Address Book stores missing birthdays as missing value (effectively null), so we should check for that rather than comparing against an early date. Thus, to get a list of the birthdayless people, we write every person whose birth date is missing value, which does exactly what it says. The whose clause filters the preceding list so that it only contains values matching the given predicate. We then add that list to the new group, which we create inline. Finally, save addressbook saves the changes and makes them visible.
However, in this case, you don't need an AppleScript at all. Address Book supports smart groups; to create one, option-click the new group button. Choose "No Birthdays" for the smart group's name, and tell it to match cards for which "Birthday" (from the first dropdown) "is not set" (from the second dropdown). This will give you a dynamically-updating group of people who have no set birthday.
Edit: It seems that while add takes a list on Leopard, it doesn't on Snow Leopard, so we'll need to use an explicit repeat loop (which works on Leopard too):
tell application "Address Book"
set noBirthdays to make new group with properties {name:"No Birthday"}
repeat with p in (every person whose birth date is missing value)
add p to noBirthdays
end repeat
save addressbook
end tell
This works almost the same way as the above solution, except we make the group beforehand, and then add each item individually. Rather than iterating over the indices, we iterate over the elements directly, since that's all we care about here.
Edit: For other properties, things are similar; in fact, when using the smart group, things are identical. From the AppleScript side, the question is the format of the data. Most slots have missing value when they're unset, but email is an exception. As we can see by testing, they're stored in a list, so we want every person whose email is {}. However, this doesn't work for me—probably because email is a list, but I'm not sure—so you'll instead want
repeat with p in every person
if email of p is {} then add p to noEmails
end repeat
And voilà, everything should work.
In- and excluding businesses is also easy, but as far as I can tell, Address Book doesn't provide a way to create a smart group for them. However, the relevant property is company, which is true for businesses and false for others. Thus, to create a list without companies, you want to do
repeat with p in (every person whose company is false)
add p to noCompanies
end repeat
If you want one master list with all of these criteria, there are two ways. First, from AppleScript, you take the two condition and and them, then add conditioning on whether or not they have an email:
repeat with p in (every person whose company is false and ¬
birth date is missing value)
if email of p is {} then add p to masterGroup
end repeat
The other option is to do this from Address Book. You first need to create a No Companies group the AppleScript way. You then create a smart group matching all of three conditions (you add them with the + button): "Email is not set", "Birthday is not set", and "Card is a member of 'No Companies'". This is probably the best option, although it's unfortunate that you have to use AppleScript for the No Companies group (I feel like there should be a way, but I can't see what it is).
Related
My Apple Music library is too big. I want to weed it out by removing a whole load of tracks that I have never listened to. I already did the same thing successfully with playlists but my script isn't working to remove tracks:
tell application "Music"
activate
set mytracks_list to (get the id of (every track whose loved is false and played count is 0 and rating is less than 60))
repeat with mytrack_id in mytracks_list
delete (the track whose id is mytrack_id)
end repeat
end tell
The mytracks_list is populated with no problems. The error message I get is:
error "Can’t get track whose id = item 1 of {130098, [............] }
Am I doing something wrong, and can it be made to work?
P.S. This is what worked for my playlists:
tell application "Music"
activate
set myplaylists_to_delete to (get the name of every playlist whose name does not contain "Adrian" and name does not contain "Loved" and name does not contain "Shazam" and name does not contain "Library" and name is not "Music" and name does not contain "Recent" and name does not contain "5 Stars" and name does not contain "Duo")
repeat with myplaylist in myplaylists_to_delete
delete playlist myplaylist
end repeat
end tell
Did you try:
tell app "Music"
delete every track whose loved is false and played count is 0 and rating is less than 60
end tell
Well-designed, well-implemented “AppleScriptable" apps can usually apply a command to multiple objects; you don’t need to iterate the objects yourself. (Hint: Apple event IPC = RPC + queries, not OOP.)
I have written an AppleScript that is activated by a mail rule whenever an email comes in that contains "+".
Why? I host my own mail server that allows for address tagging. What this means is that for example when I'm at a store and they ask for my email address, so they can email the receipt, I can give them my email address like this: whatmyemailnormallyis+nameofstore#domain.com. The applescript should then get the string between the "+" and "#" character, create a mailbox called "nameofstore" and move the message to it. Everything works fine except for I'm getting the following error:
"Mail got an error: Can’t set text item delimiters to {"+", "#"}."
This is my script:
tell application "Mail"
set unreadmessages to the first message of mailbox "INBOX" of account "Account"
set theEmail to extract address from sender of item 1 of unreadmessages
set mystring to theEmail
set text item delimiters to {"+", "#"}
set textlist to text items of mystring
set mylist to {}
repeat with i from 2 to count of textlist by 2
set end of mylist to item i of textlist
end repeat
get mylist
set mailboxName to mylist
set messageAccount to account of (mailbox of item 1 of unreadmessages)
set newMailbox to make new mailbox at (end of mailboxes of messageAccount) with properties {name:mailboxName}
repeat with eachMessage in unreadmessages
set mailbox of eachMessage to newMailbox
end repeat
end tell
When I run only the text extract portion of the script it works fine:
set mystring to "whatmyemailnormallyis+nameofstore#domain.com"
set text item delimiters to {"+", "#"}
set textlist to text items of mystring
set mylist to {}
repeat with i from 2 to count of textlist by 2
set end of mylist to item i of textlist
end repeat
get mylist
result:
{"nameofstore"}
Any help would be greatly appreciated. If anyone with better AppleScript skills than me can improve the script in other areas that would also be greatly appreciated.
This is an inheritance problem. The property (text item delimiters) is a property of the current application (AppleScript) instance, but it's being referenced unqualified inside the tell application block that directs commands to and enumerates properties from the target application, in this case Mail.
The temptation might be to set the text item delimiters outside the tell block, by moving it from its current line position to just before the block declaration. That's reasonable, but I think you've positioned it perfectly, as it's important (and good practice) to keep track of this property to ensure it's always appropriately set and, more significantly, never inappropriately not set. The most reliable way to do this, which also makes it easier to follow for other people, is to do as you've done, which is to set the property immediately prior to any statement where it exerts influence (namely, any time a list is coerced to text, or text is split into text items).
So, to avoid shuffling lines of code around, we need to be able to make it clear to the compiler that the property that we're referencing doesn't belong to application "Mail", but to the top-level scripting object, which will most typically be the current application. The three ways to do this are:
set AppleScript's text item delimiters to ...
set the current application's text item delimiters to ...
set my text item delimiters to ...
Stylistically, I favour the last option. However, my is not a synonym for current application nor for AppleScript, but rather a reference to the parent of your script. Unless the parent property is specifically declared in your script, then it will default to current application. However, there are reasons one might choose to assign a different value, in which case only the first or second of the above three options will be viable.
Here's a slight reworking of your script, which I'm afraid you'll have to test in lieu of my purchase of a new MacBook. I noticed some oddities in yours:
You obtain the first message in the inbox, but on the next line, reference item 1... of, what I imagine you thought would be a list of messages, but would in fact be a single message class object.
This single message object is the only message your script utilises to process the sender's email address. However, later on, you loop through, again, what you expect to be a collection of inbox messages, which, if it were, may not all have the same + tag.
You loopp through the text items generated by splitting the email address, and quite smartly start at index 2, and skip over every other item in the list. However, this list will only have three items in it, so there's never any looping to be done, and you can simply make use of text item 2.
In creating a new mailbox, you didn't first check to see if the mailbox already exists. I'm not sure whether Mail would throw an error, or silently ignore this. But I've redrafted the line to check first, and create if necessary.
Lastly, the final repeat loop is presently not necessary given unreadmessages is not a list (so might actually throw an error). So I removed the loop construct, but otherwise kept the line as it was. I'm not sure whether the mailbox property of a message is one that can be set, i.e. it might be read-only. If this is the case, that will throw an error, and you'll have to invoke the move command in order to move a message to a new mailbox. I may be wrong, though, and it may work just the way you intended.
tell application "Mail"
set firstInboxMessage to the first message in the inbox
set theEmail to extract address from sender of the firstInboxMessage
set my text item delimiters to {"+", "#"}
-- Since the script will trigger only when an email address
-- contains "+", we know text item 2 will always exist and
-- will always represent the slice of text we're after
set mailboxName to text item 2 of theEmail
set messageAccount to account of mailbox of the firstInboxMessage
tell the messageAccount to if the name of its mailboxes does not contain ¬
the mailboxName then make new mailbox at the end of its mailboxes ¬
with properties {name:mailboxName}
set newMailbox to the mailbox named mailboxName in the messageAccount
set the mailbox of the firstInboxMessage to the newMailbox
end tell
In reality, if you're invoking this script as a mail rule, you'll probably want to enclose this within the special handler that you can look up in the Mail scripting dictionary, called something like on receiving messages <messages> for mailbox rule <rule>
Is there a way to retrieve result of an Automator app script in an external Applescript app (not the Applescript lines in Automator)?
Something like:
tell application "My_Automator_App"
-- suppose My_Automator_App checks the Calendar to see if there some events today
-- "Show Result" in Automator will display a list
get the_Result -- list returned by Automator
end tell
I looked into this a little bit and didn't find a natural means by which AppleScript and Automator applets can communicate, although this doesn't mean one definitely doesn't exist.
In the meantime, you could implement one of a couple of workarounds/hacks that, although a little unseemly in their methods, do achieve the desired result without creating any side issues that would affect the functionality of an applet itself.
1. Use The Clipboard
Append a Copy to Clipboard action at the end of the applet's workflow, or following the action whose result you would wish to be reported.
Retrieving the clipboard from AppleScript is simple:
get the clipboard
This will probably suit return values that are simple text strings or a number. Passing an array of items from an Automator action to the clipboard isn't very reliable, sometimes only allowing access to the first item. However, this can be resolved with a small AppleScript within the workflow to process results arrays properly and convert them into an accessible format, e.g. a comma-delimited string.
However, the clipboard is also capable of storing image data, file references, and other data types, so it will be possible (if not always straightforward) to send those to be retrieved in an AppleScript.
Where possible, strings and numbers are the safest storage types.
2. Write Out To A Temporary File
To avoid using the clipboard as an intermediary, or if you wish the applet to report multiple variables without too much work, then writing the data to a temporary file is a fairly common practice, such as is done in shell scripts when persistant values are needed between multiple executions of the same script.
There's actually a special directory that gets periodically purged so that temporary data files don't accumulate: /tmp. It's hidden in Finder, but you can still create files and delete them as you would any other directory. Files that aren't access for 3 days get purged by the system.
There is a New Text File action that can write text to a file:
Specifying the /tmp directory is most easily done by creating a variable whose value is "/tmp" (without the quotes), and dragging that variable onto the appropriate field.
But my inclination would be to insert an AppleScript, or more suitably, a shell script into the workflow, with which file manipulation becomes easy and more capable.
Calendar Events Example
Using a similar example to the scenario you described, a simple applet that retrieves calendar events might have a workflow that looks like this:
where you can calibrate the first action to isolate the events you want, such as today's events. That action returns a type of object that isn't easily processed by AppleScript, but the second action extracts the relevant data in text format, summarising the list of events that the first action returned.
This is where a temporary file is useful to write out the data to a text file, which can then be retrieved in an AppleScript.
Given this Automator applet saved under the named "CalEvents", this AppleScript makes use of that applet and its result:
property tidEvents : [linefeed, linefeed, "EVENT", space] as text
property tidDetails : {tab, " to "}
property tid : a reference to my text item delimiters
run application id "com.apple.automator.CalEvents"
set tid's contents to tidEvents
set EventsSummary to read POSIX file "/tmp/EventsSummary.txt"
set EventsList to the EventsSummary's text items
set [[n], EventsList] to [it, rest] of EventsList
set n to n's last word as number
EventsList -- The final list of events from first to last
Upon its first run, the applet requires consent to access your calendar information, which only needs to be done once and will make the above script appear to fail. Once authorised, you can run the script as often as you like to get the most up-to-date content of the /tmp/EventsSummary.txt file.
Each item in the list variable EventsList is a block of text that looks like this (asterisks are my redactions for privacy, as are the address items in curly braces):
4 OF 8
Summary: GP Appointment
Status: none
Date: 07/12/2017 to 07/12/2017
Time: 14:45:00 to 15:45:00
Location: ******** Medical Centre
{Address Line 1}
{Address Line 2}
{County}
{Post Code}
United Kingdom
Notes: 01*** *****9
Each value is separated from the preceding colon by a tab character, which won't be obvious here. Also, as you can tell from the date format and address, these are British-formatted values, but yours will, of course, be whatever they are set as in Calendar.
But since each list item is much the same, extracting details for a particular event will be simple in AppleScript, first by splitting a particular event item into paragraphs, and then splitting a particular paragraph by either a tab or space character (or both) or some preposition that naturally delimits useful bits of text:
set |Event| to some item in the EventsList
set tid's contents to tidDetails
set EventDetails to {title:text item 2 of paragraph 2 ¬
, startTime:text item 2 of paragraph 5 ¬
, EndTime:text item 3 of paragraph 5} of the |Event|
which places the important event details, such as its name and start/end times, in an AppleScript record:
{title:"GP Appointment", startTime:"15:45:00", EndTime:"16:00:00"}
Working on a complex AppleScript for iTunes. One task is to accumulate a list of all playlists which contain a given track. I have this track object from somewhere else (a selection or whatever).
Currently, I've got a snippet something like this:
on containingPlaylists(theTrack)
tell application "iTunes"
set librarySource to the source named "Library"
set candidateLists to every user playlist in librarySource
set candidateId to (get id of theTrack)
set matchLists to {}
repeat with candidateList in candidateLists
set matchTracks to (file tracks in candidateList whose id = candidateId)
if (count of matchTracks) > 0 then
copy candidateList to end of matchLists
end if
end repeat
return matchLists
end tell
end containingPlaylists
This works but requires one Apple Event per playlist in the loop, which is expensive (perf) and throws away the intermediate results. What I'd RATHER do is something all in one query:
set matchLists to every playlist in librarySource whose file tracks contain theTrack
But this of course doesn't work (the particular error is "Handler only handles single objects." but not sure if that's insightful). I'm really just not sure if the language/app supports a query like this.
Can anyone confirm/deny/offer any insight? Thanks!
You can use this (work on iTunes 11 and 12):
tell application "iTunes"
set theTrack to item 1 of (get selection)
return user playlists of theTrack
end tell
Updated --
In the AppleScript dictionary:
artwork n [inh. item] : a piece of art within a track |
elements : contained by tracks. So artworks of thisTrack works
track n [inh. item] : playable audio source |
elements : contains artworks; contained by playlists. So playlists of thisTrack works, you can use user playlists of thisTrack
In iTunes.h (ObjC scripting bridge):
#interface iTunesTrack : iTunesItem
- (SBElementArray *) artworks;
it's not possible because playlists is not in the SBElementArray's list.
But I do not know why there is a difference between the AppleScript dictionary and the iTunes.h file.
I too wish a whose clause like that could be used. But alas. Someone else might come up with a better plan, but I'm pretty sure this is how I would find the playlists containing the selected track (it may be the most efficient):
set persisID to persistent ID of selection
set pp to playlists
set playListsWithIt to {}
repeat with p in pp
set tt to (tracks of p whose persistent ID is persisID)
if tt ≠ {} then set playListsWithIt to (playListsWithIt & (id of p))
end repeat
Then I can use those IDs for the next step. This includes, of course, playlists like "Recently Added", which may or may not be what you want; you'd have to put another step in there to 'filter' out such a result.
I'm writing an Applescript for use in iTunes in which at some point I want to select any track from a list of tracks, but the way I expected it to work gives an error. Here's the code:
tell application "iTunes"
set thePlaylist to the first playlist whose name is "Missing track count"
-- ...
-- populate a list of strings: albumList
-- ...
repeat with albumName in the albumList
set theAlbum to (the tracks of thePlaylist whose album is albumName)
display dialog "Found " & (count theAlbum) & " tracks in the album"
set aTrack to some track of theAlbum -- ERROR OCCURS HERE
end repeat
end tell
The error I get when I execute the script from within iTunes is:
Can't get some «class cTrk» of {«class cFlT» id 16112 of «class cUsP» id 15982 of «class cSrc» id 65 of application "iTunes", ... etc}
Now, I don't really see why it doesn't work, although I guess it must have something to do with the fact that the items in theAlbum are file tracks from a user playlist from the source from the iTunes application instead of 'just' tracks. Can anyone help me out here?
In this example I use some item instead of some track, which works OK.
tell application "iTunes"
set thePlaylist to the first playlist
set x to (the tracks of thePlaylist)
set aTrack to some item in x
end tell
results in
URL track id 87 of library playlist id 82 of source id 64 of application "iTunes"
Since all the items in your example inherit from track, I don't know why it doesn't work, but it doesn't.
theAlbum is a list, not a playlist, so it doesn't have track elements; it only has items.
The documentation on lists, where it states "You can also refer to indexed list items by class." is incomplete and thus misleading. It seems you can only do this with to built-in classes. From what I can glean, here's why:
Object specifiers (2) are based on key-value coding. A specifier might identify a property (an object attribute or a to-one relationship) or element (a to-many relationship). In the example, we're dealing with elements. To handle elements, the underlying Objective-C class must implement a collection accessor pattern. That is, it must implement at least -<key>, or -countOf<Key> and -objectIn<Key>AtIndex: (it can, of course, implement all of them). The list class does this for a set number of Applescript classes (if you peeked at the ObjC source for the list class, you'd find methods like countOfApplication and -objectInNumberAtIndex:). It could conceivably support arbitrary element object specifiers with an appropriate -doesNotRecognizeSelector: handler, but lists don't appear to have been implemented this way. Since lists don't have -track, -countOfTrack or -objectInTrackAtIndex: handlers, they can't deal with a specifier such as "first track of trackList".