I’m trying to set the location of a track but it gives me a “Can’t convert types” error when I try to set it to a path with whitespaces.
Application('Music').tracks.whose({name: "audio"})[0].location(); // "/path/to/audio.mp3"
Application('Music').tracks.whose({name: "audio"})[0].location = "/new/path/to/audio.mp3"; // "/new/path/to/audio.mp3"
Application('Music').tracks.whose({name: "audio"})[0].location = "/new/path/with whitespace/to/audio.mp3"; // Error -1700: Can't convert types
// Even with Path().toString() instead of a path string it won't work
Thanks in advance for answering!
Model: MacBook Pro (13-inch, 2016, Two Thunderbolt 3 ports)
AppleScript: 2.7
Browser: Firefox 78.0
Operating System: macOS 10.14
==============
SOLVED:
Changing the whitespaces into %20 seems to work!
Application('Music').tracks.whose({name: 'audio'})[0].location = "/new/path/with%20whitespace/to/audio.mp3";
// I recommend using the encodeURI function
Application('Music').tracks.whose({name: 'audio'})[0].location = encodeURI("/new/path/with whitespace/to/audio.mp3");
When you need to interact with files, such as a document in TextEdit, you will need a path object, not just a string with a path in it. You can use the Path constructor to instantiate paths.
TextEdit = Application(‘TextEdit’)
path = Path(‘/Users/username/Desktop/foo.rtf’)
TextEdit.open(path)
Note: Get the string value of the Path by calling the toString method.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 8 juin 2020 10:31:32
I just tried with the Path object, but it gives me this error: “Error: Error: Can’t get object.”
var track = Application('Music').tracks.whose({name: 'audio'})[0];
track.location = Path("/new/path/with whitespace/to/audio.mp3"); // Error: Error: Can't get object.
Looks like it will only work with Applescript…
tell application "Music"
set location of tracks whose name is equal to "audio" to "/new/path/with whitespace/to/audio.mp3" # {application "Music"}
end tell