Check for multiple object properties in AppleScript - applescript

I wrote an AppleScript that shall check existing reminders in the Reminders app for their name. If a reminder with the given name (Erinnerung) does not exist one shall be created.
But the following code does check the uncompleted reminders only:
tell application "Reminders"
if exists (reminders whose name is Erinnerung) then
else
set newremin to make new reminder
set name of newremin to Erinnerung
end if
end tell
So I tried to check for the name and the completion status at once:
tell application "Reminders"
if exists (reminders with properties {name:Erinnerung, completed:true}) then
else
set newremin to make new reminder
set name of newremin to Erinnerung
end if
end tell
But then I'm getting an error:
"„properties:true“ kann nicht diesem „reminders“ folgen."
("„properties:true“ can not follow this „reminders“")
Why does the Reminders app check for uncompleted reminders only? And why can't I check for two properties at once? Does anyone know what I am doing wrong?

To filter objects by one or more properties filter the array and count the elements of the result.
One property (I changed the variable Erinnerung to a literal string)
tell application "Reminders"
set erinnerungen to reminders whose name is "Erinnerung"
if (count erinnerungen) is 0 then
set newremin to make new reminder
set name of newremin to "Erinnerung"
end if
end tell
Two properties
tell application "Reminders"
set erinnerungen to reminders whose name is "Erinnerung" and completed is true
if (count erinnerungen) is 0 then
set newremin to make new reminder
set name of newremin to "Erinnerung"
end if
end tell
If you want to check an object which can be referenced by name you can also use this
tell application "Reminders"
if not (exists reminder "Erinnerung") then
set newremin to make new reminder
set name of newremin to "Erinnerung"
end if
end tell

Related

apple script- email transmission containing groups of 20 attachments taken from a folder containing 1000 files

sorry I didn't paste my piece of code but I don't know where to start... I'm talking about apple script. I've found some code to send emails and I would like to implement a script that does what I'm about to write.
I have a folder that contains about 1000 pdf files that I have to send in groups of 20 at a time via email.
I have a bit of confusion in my head but conceptually I think it can be done by counting all the files in the folder, then every 20 files I take the respective file names and set a variable with these names, including the path and I send the first email, then I proceed with the other 20 and so on until it has completed the number of files in the folder. Is there someone who can help me? A thousand thanks!!
I am attaching the portion of code relating to sending via email...
I tried the given code and it works. it sends the email but i didn't check if it sends attachment
`
tell application "Mail"
set theFrom to ""
set theTos to {""}
set theCcs to {}
set theBccs to {}
set theSubject to ""
set theContent to ""
set theSignature to ""
set theAttachments to {}
set theDelay to 1
set theMessage to make new outgoing message with properties {sender:theFrom, subject:theSubject, content:theContent, visible:false}
tell theMessage
repeat with theTo in theTos
make new recipient at end of to recipients with properties {address:theTo}
end repeat
repeat with theCc in theCcs
make new cc recipient at end of cc recipients with properties {address:theCc}
end repeat
repeat with theBcc in theBccs
make new bcc recipient at end of bcc recipients with properties {address:theBcc}
end repeat
repeat with theAttachment in theAttachments
make new attachment with properties {file name:theAttachment as alias} at after last paragraph
delay theDelay
end repeat
end tell
# macOS 10.12+ know bug
# https://stackoverflow.com/questions/39860859/setting-message-signature-of-outgoing-message-using-applescript
# set message signature of theMessage to signature theSignature
send theMessage
end tell
`

Using dynamic fields in an Alfred Workflow to create a new email in Mail.app

I'd like to add dynamic dates into my Alfred Workflow, but can't figure it out. This is what I've got so far:
tell application "Mail"
activate
make new outgoing message with properties {subject:"some subject", content:"some content" & return & return}
end tell
I'd like to include a dynamic date in the subject and Alfred snippets in the content, but they're not being recognized. For example:
tell application "Mail"
activate
make new outgoing message with properties {subject:"{date} Call Follow Up", content:"some content" & return & return}
end tell
Does that make sense? Any help would be much appreciated!
I am not sure if I understand your problem correctly.
Are you trying to accomplish something like this?
set mySubject to ((current date) as text) & " Call Follow Up"
tell application "Mail"
activate
make new outgoing message with properties {subject:mySubject, content:"some content" & return & return}
end tell
Apparently, you can't access Alfred variables in the Run Script workflow object. So, you can do something like this:
1. Add a snippet object
2. Connect to it a keyword input object
3. Connect a run script object with this code:
on run argv
set theQuery to item 1 of argv
set theSubject to date string of (current date) & " Call Follow up"
tell application "Mail"
activate
make new outgoing message with properties {subject:theSubject, content:theQuery & return & return}
end tell
end run

Applescript for mail rules

I have created a rule that messages move into a subfolder when they according to certain criteria.
Additionally, I have saved in this rule an AppleScript that this mail copied subsequently into an DMS.
It is working.
My problem is, that the name of the subfolder is (for now) not passed to the script. It contains only "INBOX":
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with theMessage in theMessages
set nameAccount to name of mailbox of theMessage # here is the problem !
try
tell theMessage
— at this point get the data out of the mail
end tell
tell application id „DMS“
— here the data will save to the dms
end tell
end try
end repeat
end tell
# end try
# end tell
end perform mail action with messages
end using terms from
How do I get the name of the destination folder from the active mail rule?
Instead of the name get the whole mailbox object, it contains the actual reference in the folder hierarchy, to create a subfolder you need the reference anyway.
set theMailbox to mailbox of theMessage
PS: the tell application "Mail" block is not needed, the using terms block is sufficient to evaluate the terminology.

ScriptingBridge iTunesTrack VS. iTunesFileTrack - Sandbox Error

I have an AppleScript-ObjC app, which is sandboxed, and am running into an issue with trying to get or set properties of an iTunes track. Oddly, I can successfully run the get/set methods when working with an iTunesTrack object, but cannot when working with an iTunesFileTrack object.
Example:
set iTunesAppObject to current application's SBApplication's applicationWithBundleIdentifier_("com.apple.iTunes")
set trackToEdit to iTunesAppObject's currentTrack()
set trackPersistentID to trackToEdit's persistentID()
trackToEdit's setName_("TEST NAME") -- this works, trackToEdit is an iTunesTrack object
-- GET ALL SOURCES FROM ITUNES
set iTunesSources to iTunesAppObject's sources()
-- REPEAT FOR EVERY SOURCE
repeat with aSource in iTunesSources
-- LOOK FOR MAIN LIBRARY SOURCE
if (aSource's |kind|()) is 1.800169826E+9 then
set mainLibrary to aSource
exit repeat
end if
end repeat
set libraryPlaylist to mainLibrary's |libraryPlaylists|() -- GET LIBRARY PLAYLIST
set libraryPlaylist to libraryPlaylist's objectAtIndex_(0)
set allTracksInPlaylist to libraryPlaylist's |fileTracks|() -- GET ALL TRACKS IN PLAYLIST
-- SET UP PREDICATE TO FIND TRACK WITH ID
set searchPredicate to current application's NSPredicate's predicateWithFormat_(("persistentID == " & quoted form of (trackPersistentID as text)))
allTracksInPlaylist's filterUsingPredicate_(searchPredicate) -- FILTER ARRAY
set trackToEdit to allTracksInPlaylist's objectAtIndex_(0) -- GET REMAINING ITEM IN ARRAY, RETURNS iTunesFileTrack OBJECT
log trackToEdit -- returns a seemingly valid iTunesFileTrack object
trackToEdit's setName_("TEST NAME") -- does NOT work, result in sandbox dispatch deny error in Console.app
log trackToEdit's |name|() -- does NOT work, returns null/missing value
Everything works when the app is not sandboxed. I have added pretty much every entitlement to my project, including:
com.apple.iTunes.library.read-write
com.apple.iTunes.user-interface
com.apple.iTunes.playerInfo
com.apple.iTunes.playback
Can anyone think of a way to get around this? Is is possible to "convert" an iTunesFileTrack object to an iTunesTrack object? Thanks in advance for any suggestions!
bless your heart. try this out:
property aTrack : missing value
on getTrack_(sender)
tell application "iTunes"
set aTrack to (get current track)
end tell
end getTrack_
on renameTrack_(sender)
set name of aTrack to "whatever"
end renameTrack_

Can new items in Reminders.app be created with a fixed order using Applescript?

I would like to be able to add items to a list in Reminders in a specific order, using AppleScript.
Using the following script, however, adds the items in a different order each time I run it:
set my_reminders to {"item4", "item3", "item2", "item1", "item"}
tell application "Reminders"
tell list "Reminders"
repeat with the_name in my_reminders
set this_reminder to make new reminder with properties {name:the_name as string}
end repeat
end tell
end tell
It seems that adding a short delay after the reminder creation solves the problem:
set my_reminders to {"item4", "item", "item2", "item1", "item3"}
tell application "Reminders"
tell list "Reminders"
repeat with the_name in my_reminders
set this_reminder to make new reminder with properties {name:the_name as string}
delay 1
end repeat
end tell
end tell

Resources