ScriptingBridge iTunesTrack VS. iTunesFileTrack - Sandbox Error - applescript

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_

Related

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.

Select a type of sender for email with wildcard

I would like to send an email with applescript. This script will be used on a few computers and the email should be sent by a certain type of account containing "mynetwork.com" in the email address.
Is there a way to automatically select the computer email account containing "my network.com" ? Obviously the wildcard * is not working see code below
property faxboxEmail : {"fax#opilbox.com"}
property theNumber : ""
property theContent : ""
property theSender : "*#mynetwork.com"
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:theNumber, content:theContent, sender:theSender}
tell newMessage
make new to recipient with properties {address:faxboxEmail}
end tell
end tell
The sender address for accounts in Mac OS X Mail are stored in the sender addresses property in an account. So, technically what you want is something like get account whose email addresses ends with "#mynetwork.com". However, the email addresses property is a simple list, and AppleScript doesn’t have dynamic searches into lists like that.
However, the number of accounts on any personal computer should be fairly small, so looping through them shouldn’t be a problem.
property faxboxEmail : {"fax#opilbox.com"}
property theNumber : "Nine"
property theContent : "Hello, World"
set foundAddress to false
tell application "Mail"
repeat with potentialSender in accounts
tell potentialSender
repeat with potentialAddress in email addresses as list
if potentialAddress ends with "#mynetwork.com" then
set foundAddress to true
exit repeat
end if
end repeat
end tell
if foundAddress then
exit repeat
end if
end repeat
if foundAddress then
set senderName to full name of potentialSender
set senderAddress to senderName & " <" & potentialAddress & ">"
set newMessage to make new outgoing message with properties {visible:true, subject:theNumber, content:theContent, sender:senderAddress}
tell newMessage
make new to recipient with properties {address:faxboxEmail}
end tell
end if
end tell
You may also find it useful to look at the properties of each account, using something like:
tell application "Mail"
--or “account 1”, “account 3”, etc., up to the number of accounts
get properties of account 2
end tell
If you run that and then look in the results pane, you’ll see all of the properties, including the email addresses property. If your script isn’t doing what you expect it to do, post not just the script, but also the values of the property you’re looking at, in this case the email addresses property. You will probably want to use a test computer with fake example.com, example.org, and example.net email addresses, so as not to expose real email addresses to harvesters.
You also may find it easier to build your scripts from the ground up. For example, in this case, you would want to write a script that sends a hard-coded email from a hard-coded sender. Only once that aspect of the script is working, would you want to add the wrinkle about searching for a dynamic sender. This will simplify your programming process by making each task smaller, and will also make it more obvious where the code is going astray.

Check for multiple object properties in 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

Applescript can't make <list> into type specifier -1700 error

Can anyone explain why the following piece of Applescript code returns an error:
tell application "Photos"
set idList to {"v3xzjwPQT0y8a844gLHLHWg", "v3w3twPQT0y%%844gLgf4Wg"}
set itemList to every media item in application "Photos" where its id is in idList
log (count of itemList)
end tell
The error is:
error "Photos got an error: Can’t make {\"v3xzjwPQT0y8a844gLHLHWg\", \"v3w3twPQT0y%%844gLgf4Wg\"} into type specifier." number -1700 from {"v3xzjwPQT0y8a844gLHLHWg", "v3w3twPQT0y%%844gLgf4Wg"} to specifier
There is no error in this line of code using OSX 10.9 and iPhoto. Perhaps something changed in "Photos"/10.10?
In any event, do you still get the error if you move the line of code
set idList to {"v3xzjwPQT0y8a844gLHLHWg", "v3w3twPQT0y%%844gLgf4Wg"}
to just before the Tell "Photos" block? The error is saying "Photos" got the error trying to make the idList. So try defining the idList without Photos. I would think the list could be defined without telling Photos to do it.
I have the same issue. My goal is to get all images that are not in a certain album.
The error rises in the line
set itemList to every media item in application "Photos" where its id is in idList
Also this one-liner results in an error
set itemList to every media item in application "Photos" where its name is in {"bla"}
What works is the following line
set itemList to every media item in application "Photos" where its name is "bla"
But that does not solve the problem. Maybe someone has a suggestion how to do it.
That feature does seem to be broken in Photos 4. You can work around it in a couple of ways. If your list is simple and fixed you can build up a command using or statements like so:
set itemList to every media item whose id is "wi9HoFU0SKCDianaQBtJBg" or id is "DT29HF1lRgCKOaIZFHoHrQ"
If you need something more flexible, you can use a repeat loop and build up the list through individual searches:
set idList to {"wi9HoFU0SKCDianaQBtJBg", "DT29HF1lRgCKOaIZFHoHrQ"}
tell application "Photos"
set newList to {}
repeat with thisID in idList
set anItem to (first media item whose id is thisID)
copy anItem to end of newList
end repeat
end tell

How do I check if a key in a record exists?

I discovered that if I try to access a field in a record, e.g.:
set track_album to (|Album| of t)
And that field does not exist, AppleScript throws an error. How can I check if that field exists first? Or how to let it fail silently? (Whichever is the best practice.)
set track_album to album of (t & {album:default})
it will be set to album if it exists or default if not.
or
try
album of t
on error -1728
default
end try

Resources