Howto show a certain recurring event in iCal with Applescript? - macos

How can I show a certain recurring event in iCal with Applescript?
The code I use currently shows the first occurrence of that event, not the one I'm interested in.
tell application "iCal"
tell calendar CALENDAR_NAME
set theEvent to first event whose uid = SOME_UID
end tell
if theEvent is not null then
show theEvent
end if
end tell

It's complicated. Look at this samplescript from MacScripter. It's a pretty impressive script.
http://macscripter.net/viewtopic.php?id=29516

Related

How do I create calender alarms using Applescript (using OSX Yosemite/Mavericks)?

I have an Applescript here that triggers when an ICS file has been dropped into a folder. All it does it imports the ICS file into the Calendar and then create alarms for each calendar entry.
This was working fine on Mountain Lion. But hasn't been working since Mavericks/Yosemite. The calendar entries are being imported. But no alarms are being created.
This is code I use to add the alarm:
tell application "Calendar"
tell calendar "Home"
delay 1
set myevents to every event
repeat with oneevent in myevents
tell oneevent
set mysummary to summary
display dialog mysummary
make new display alarm at end with properties {trigger interval:-30}
end tell
end repeat
end tell
end tell
As you can see from the code, I am displaying the summary for the event just as a test. I just want to find out it's opening the correct event.
I've looked/searched for examples/documentation on the topic but hardly find anything recent (i.e. Mavericks and above).
Any ideas on how to fix my problem? Thanks a lot
try this
tell application "Calendar"
tell calendar "Home"
repeat with oneevent in (get every event)
tell oneevent
set startDate to start date
set newAlarm to make new display alarm at end of display alarms with properties {trigger date:startDate}
set trigger interval of newAlarm to -30
end tell
end repeat
end tell
end tell

AppleScript to get the active list in Reminders?

Can anyone help me to get the active list on display in the Reminders app on OS X?
According to the applescript dictionary for reminders, the application has a "default list" property which is "the list currently active in the Reminders application."
However, this property always seems to return the first list in order in the lists sidebar, not the list which is actually being displayed and is active. I have found that if I rearrange the order of the lists in the sidebar, I will always get whichever I have made the first list, regardless of which is actually being viewed and worked with.
My application is to create a Keyboard Maestro trigger to run an AppleScript to print the list I am currently working on, but it does not appear that the Reminders app functions as is documented in its dictionary. (I have temporarily used a workaround of having the script pop up a chooser listing all the lists so I can select the one i want to print, but that's inefficient and inelegant).
Thanks!
Yes, you can, but you will have to use the bad GUI scripting. And in a bad way. Look:
--Do some GUI scripting to get the decription of a specific group
tell application "Reminders" to activate
tell application "System Events"
tell process "Reminders"
tell window "Reminders"
tell splitter group 1
tell group 1
set des to get description
end tell
end tell
end tell
end tell
end tell
--This description is in the format "Viewing MyList, 1 reminder" so get the part to the "," from des.
set text item delimiters to ","
set texitems to text items of des
set firstPart to get text item 1 of texitems
--Setting the delimiters back
set text item delimiters to ""
--Jump to charcter 9 of firstPart then converting to text
set listname to characters 9 thru end of firstPart as text
--Now we know the name of the current list, so do whatever you want:
tell application "Reminders" to get list listname
This works. But only if Reminders is open. And if Apple changes Reminders structure...

How do I stop AppleScript from selecting all messages in a Mail.app conversation when I only want one from the group?

Here's my itch: I want to add emails to Reminders.app as todo items. I figured that part out. My next goal was to be able to select multiple emails and have Reminders.app create todos for each email selected. I figured that part out.
The problem: When I select an email that is part of a conversation ALL messages from that conversation are added as individual reminders/todos. This part may be confusing but I'll try to be as detailed as possible in describing how I'm selecting messages. In Mail.app I'm selecting a message in the far right-side pane where all emails that part of the conversation are presented in a scrollable list. It's this area where I'm selecting a particular message. So even though I'm selecting one message from a conversation, all messages in that conversation are added to my AppleScript list variable and, subsequently, turned into reminders/todos.
If I turn off 'Organize by Conversation' in Mail.app the problem goes away. I like the cleanliness of organizing my emails by conversation so if there's a scripting solution to this problem I'd prefer that route. However, I can't think of any way to fix this so I'm hoping someone here has some thoughts.
Here's my script:
property defaultList : "Parking Lot"
on newReminder(theBody, theTitle)
tell application "Reminders"
tell list defaultList
make new reminder with properties {body:theBody, name:theTitle}
end tell
end tell
end newReminder
tell application "Mail"
set selectedMessages to {}
set selectedMessages to selection
if (count of selectedMessages) is 0 then
return "Please select a message in Mail.app and try again."
else
repeat with i from 1 to (count of selectedMessages)
tell item i of selectedMessages
set messageid to message id
set urlText to "message://" & "%3c" & messageid & "%3e"
set theSender to extract name from sender
set theSubject to subject
my newReminder((theSender & " " & urlText), theSubject)
end tell
end repeat
end if
end tell
The AppleScript property selection for the Mail application appears to ignore whether a single message from a conversation is highlighted in the preview pane (the rightmost pane in OS X Lion's layout). The selection is determined solely by which messages are selected in the message list (the middle pane). If you want to utilize selection in your AppleScript for a single message of a conversation, you'll have to select the single message from the message list,
instead of the preview pane.

Applescript for creating New Message with Mail application

I have an AppleScript for Mail.app which opens a new message window with pre-defined recipient address and subject. This script opens a new window every time I run it:
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
end tell
But I want the script to open a new message window only when the previously opened window is closed – otherwise, the previously opened window should come to the front.
Can anyone please help me in modifying this script to achieve the above mentioned functionality?
I didn't test this but it should do what you need... at least it shows you the proper approach. You basically use a "property" to keep track of some value from the last time the script was run. In this case we check for the name of the frontmost window and see if it matches your criteria. If the window name doesn't do what you need then just find some other value to track between launches of the script. The basic approach should work.
EDIT: using the ID of the message, which is unique, the following will do what you want:
property lastWindowID : missing value
tell application "Mail"
set windowIDs to id of windows
if windowIDs does not contain lastWindowID then
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
set lastWindowID to id of window 1
else
tell window id lastWindowID
set visible to false
set visible to true
end tell
activate
end if
end tell
the visibility toggle seems to be the only way to get the window in front, as frontmost is a read-only property. The lastWindowID property will store the ID as long as the script is not re-compiled (caveat empteor: do not put this into an Automator service, as these get re-compiled every time the service is loaded).

How to set the sender of the current Mail.app outgoing message via AppleScript?

I'd like to write an AppleScript to set the sender of the current outgoing message in Apple's Mail.app.
I've tried this:
tell application "Mail" to set sender of front outgoing message to "<my email address>"
but I get the error message error "Mail got an error: Can’t set sender of item to any." number -10006 from sender of item to any which doesn't make sense to me.
When I try to interrogate the front outgoing message as follows:
tell application "Mail" to get properties of front outgoing message
I get {class:item} in return, instead of an "outgoing message" object like I'd expect.
Any ideas?
Unfortunately, you cannot get or set the properties of the outgoing message object of Mail with Applescript.
Instead, you can accomplish this with GUI scripting, a workaround that directly manipulates window elements.
This code should work for you:
tell application "System Events"
tell process "Mail"
click pop up button 1 of window 1
click menu item 6 of menu 1 of pop up button 1 of window 1
end tell
end tell
Change the menu item 6 on the fourth line to whichever number in the list your desired sender is (e.g., if the sender you want to change to with this script is the fourth one listed, change menu item 6 to menu item 4).
Update: If you're curious, since this answer is over two years old: as of 2014-04-26, getting/setting the properties of outgoing messages is still impossible and this workaround still works in OS X 10.9 Mavericks.
I think it's a little more complicated. My own experiments agree with the conclusion of Alan Kimelman in this thread, that AppleScript works as expected for outgoing messages created from scratch by the script. For example, the following code works:
tell application "Mail"
set newMessage to (a reference to (make new outgoing message))
tell newMessage
make new to recipient at beginning of to recipients ¬
with properties {address:"hello#world.com", name:"The World"}
set the sender to "Jerry Krinock <jerry#sheepsystems.com>"
set the subject to "A Test"
set the content to "This is only a test."
send
end tell
end tell
However, if the message is created by other means (for example, I create HTML messages by telling Safari to Share via Email), then Matthew McVickar is correct that AppleScript is broken. You can't set or get any properties, and also it refuses the send command.
Actually you can set the sender of an outgoing message as described here:
Incorrect sender when sending Email via Applescript
It is more than a little frustrating that you can't set the sender of an outgoing message.
Here's a more general version of Matthew's script that I wrote many years ago and have used frequently. The argument is a string that contains exactly what you see in the "From:" pop up, e.g.
"Mitchell L Model <dev#software-concepts.org>"
(It is very tricky to get the details of something like this right if you don't do a lot of GUI scripting.)
to setFrom(address)
tell application "System Events"
activate application "Mail"
tell pop up button "From:" of window 1 of application process "Mail"
click
tell menu item address of menu 1 to click
end tell
end tell
end setFrom
The reason I use a parameterized handler is that I use keystroke macro facilities to bind a keystroke combination for each of my email addresses. Each executes an AppleScript that loads the file containing the above handler and invokes it with a specific email address. For example:
property util : load script alias (((path to home folder) as text) & "Scripts:Utilities.scpt")
util's 's setFrom("Mitchell L Model <dev#software-concepts.org>")
It's trivial to use AppleScript to create a new outgoing message with any outgoing address that you like (well, at least, the ones configured in your Mail.app preferences).
The discussions here hinge around trying (incorrectly) to change it after the message is created. Instead, specify it when you create the message:
set new_m to make new outgoing message with properties {sender:"My Name <me#my.com>"}
The text in quotes needs to match both the real name and the email address (in chevrons) of any configured account. If there's no match, Mail.app will use the default address
Try this instead.
tell application "Mail" to make new outgoing message with properties {sender:"<your_email_address>", visible:true}
UPDATE: I would look in Mail's scripting dictionary and see what you can and cannot do. The information provided there might help. :)
An alternative to the GUI scripting example I provided would be an Automator script that utilizes the 'Watch Me Do' command. I don't understand exactly how it works underneath the hood, but it ostensibly records mouse movement and GUI interaction and then reenacts what you recorded it when it runs. Unfortunately, I experienced significant lag when running the script. After asking it to run, it would sometimes execute after 5 or more seconds, which is clearly unusable.
The GUI scripting method is almost instantaneous and cleaner-looking (no mouse movement) to boot. I recommend it.

Resources