AppleScript value for AXURL fails with AppleEvent handler failed - applescript

I have been trying to capture a URL in Apple Music (formally iTunes) using AppleScript (JXA JavaScript) but unfortunately when returning the value for an AXURL attribute the program.
Am I missing something on converting this URL to something useful to return?
Code (simplified to replicate issue)
tell application "System Events"
tell process "Music"
open location "itmss://music.apple.com"
set theWindow to (first window)
set theBrowser to UI element 1 of scroll area 1 of (group 1) of (group 1) of (splitter group 1) of theWindow
return value of attribute "AXURL" of theBrowser
end tell
end tell
Error
Error -10000: AppleEvent handler failed.
Console log output
Error while returning the result of a script command: the result object...
https://music.apple.com/WebObjects/MZStore.woa/wa/viewGrouping?cc=gb&id=130
...could not be converted to an Apple event descriptor of type 'specifier | number | text | list | record | any | boolean'.
This instance of the class 'NSURL' doesn't respond to -scriptingSpecifierDescriptor messages.
This instance of the class 'NSURL' doesn't respond to -scriptingNumberDescriptor messages.
This instance of the class 'NSURL' doesn't respond to -scriptingTextDescriptor messages.
This instance of the class 'NSURL' returned nil when sent -objectSpecifier (is it not overridden?) and there is no coercible type declared for the scripting class 'list'.
This instance of the class 'NSURL' doesn't respond to -scriptingRecordDescriptor messages.
This instance of the class 'NSURL' doesn't respond to -scriptingAnyDescriptor messages.
This instance of the class 'NSURL' doesn't respond to -scriptingBooleanDescriptor messages.
Thank you for your help.

Related

How do you use parameter specifications for a class from another application?

I'm trying to use parameter specifications for the application process class from System Events:
on someHandler(anInt as integer, anAppProcess as application process)
end someHandler
anInt works fine, but trying to add anAppProcess results in a Syntax Error ‘Expected “,” or “)” but found identifier.’ on the word process. I believe this is because application process is not in scope and is instead parsed as application as a single class and the token process, instead of the class application process.
I've also tried:
tell application "System Events"
on someHandler(anAppProcess as application process)
--^^ Expected “end” or “end tell” but found “on”.
end someHandler
end tell
on someHandler(anAppProcess as application "System Events"'s application process)
-- ^ Expected “,” or “)” but found “"”.
end someHandler
tell application "System Events" to set ap to application process
on someHandler(anAppProcess as ap)
-- ^^ Expected class name but found identifier.
end someHandler
Is there a way to get application process in scope or otherwise use parameter specifications for classes from other applications?
This also shouldn't be related to application process containing a space; I have tried this for single-word classes from other applications as well.
A handler must be declared on the top level of the script.
But you can use an using terms from block which is not treated as a level
using terms from application "System Events"
on someHandler(anAppProcess as application process)
end someHandler
end using terms from
But I have no idea what you are going to coerce to application process

Updating Chromecast receiver status text after start

On my Chromecast custom receiver, I can set the status text like so:
const instance = cast.framework.CastReceiverContext.getInstance();
const options = new cast.framework.CastReceiverOptions();
options.statusText = 'Custom status text!!';
instance.start(options);
This text is displayed on potential cast senders.
I'd like to be able to change this text after the receiver instance has already been started. Is this possible? If so, how can it be done?
Having the application started, is possible to use CastReceiverContext to change the application status:
// call setApplicationState and pass the new statusText
instance.setApplicationState('New custom status text');
setApplicationState(statusText)
Sets the application state. The application should call this when its
state changes. If undefined or set to an empty string, the value of
the Application Name established during application registration is
used for the application state by default.
Parameter
statusText:string
The status text.
More information on the official docs.

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.

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_

AppScript issues with Cocoa NSString and AppleScript text types?

In one of my Applications I've added a few new properties to the document class of the Standard AppleScript suite. Some of these properties have the type text, others have the type boolean. I've written a "sdef" file and added if to the resources of my App.
But when I launch the App, there are messages in the Console like this:
.sdef warning for type 'text' attribute 'title' of class 'window' in
suite 'Standard Suite': AppleScript name references may not work for
this property because its type is not NSString-derived.
And when I actually try to get the value from such a new property with type text from within an AppleScript, it will fail. The correct method within my App is called, and it returns the correct value as NSString, but the AppleScript returns with an error. There's a message in the Console again:
Error while returning the result of a script command: the result
object... "the requested NSString value" ...could not be converted to
an Apple event descriptor of type 'text'. This instance of the class
'__NSCFString' returned nil when sent -objectSpecifier (is it not
overridden?) and there is no coercible type declared for the scripting
class 'text'.
Which probably means that it was not possible to convert the Cocoa NSString object into an AppleScript text object.
When I try to get the value from one of the new boolean properties, it works just fine.
What exactly do I need to do to convert a NSString object into an AppleScript text object?
I suspect you need to coerce the text parameter into a NSString.
I.e
Set sometextString to current application's NSString's stringwithTring:atext
Use it in your method
And to return it you do.
Return sometextString as text

Resources