Select specific directory or file in Open-Save dialog

I’d like to use Applescript to tell an application (Swinsian) to import a Music playlist. I need to direct it to the XML file in a specific folder. How do I either change the directory in the Open-Save dialog or tell the app to open the specific XML file? Thanks.

You want default location:

set whereToLook to path to desktop

set selectedFile to choose file with prompt "Navigate to the file" default location whereToLook

 set newFile to choose file name with prompt "Navigate to where to save the file" default location whereToLook


Thanks, but I don’t think this is what I was looking for. This will allow me to choose a file through a choose file AppleScript dialog. I want to direct the app Swinsian to choose a specific file in an Open-Save dialog.

Are you writing that you need to copy a certain playlist from Music.app to Swinsian.app? It won’t be easy, but doable.

But what about XML? Music.app playlists are stored in a .db database. Try to explain yourself more clearly.

tell app "swinsian" to open selectedFile

OK, here goes. I want Swinsian to be able to access my Music playlists, and I want it to happen without my intervention. To do this, I export my Music library automatically daily as an XML file using the app Music Library Exporter. I want to be able to write an Applescript app that will launch daily after the XML file is created, and tell Swinsian to import the XML file. There is a Swinsian menu command to import the file - I have written an Applescript to select that menu item. What I need is a way for Applescript to then direct Swinsian to the XML file and select it.

It is not possible to add a playlist directly. You will have to first export the playlist from Music.app to some folder on the disk. For example, like this:
 

tell application "Music" -- or "iTunes"
	set sourcesNames to name of every source
	set theChoice to choose from list sourcesNames with prompt "PLEASE SELECT THE SOURCE"
	if theChoice is false then return
	set theSourceName to item 1 of theChoice
	set playlistNames to name of every playlist of source theSourceName
	set theChoice to choose from list playlistNames with prompt "PLEASE SELECT THE PLAYLIST(s)" with multiple selections allowed
	if theChoice is false then return
	set playlistsFolder to my makeDestinationRootFolder()
	repeat with i from 1 to count theChoice
		set selectedPlayList to playlist (item i of theChoice) of source theSourceName
		if (count (get tracks of selectedPlayList)) > 0 then
			set fileLoci to {}
			try
				set view of front browser window to selectedPlayList
				set visible of front browser window to true
			end try
			set playlistName to "playlist \"" & name of selectedPlayList & "\""
			say "Exporting " & playlistName
			tell selectedPlayList to repeat with aTrack in tracks
				tell aTrack to if not ((get its location) is missing value) then set fileLoci's end to its location
			end repeat
			set currentFolder to my makeDestinationPlaylistFolder(playlistName)
			tell application "Finder" to duplicate fileLoci to currentFolder
		end if
	end repeat
	quit
end tell

on makeDestinationPlaylistFolder(playlistName)
	tell application "Finder"
		try
			set currentFolder to make new folder at folder "Exported PlayLists" of desktop with properties {name:playlistName}
		on error
			set currentFolder to folder playlistName of folder "Exported PlayLists" of desktop
		end try
	end tell
	return currentFolder
end makeDestinationPlaylistFolder

on makeDestinationRootFolder()
	tell application "Finder"
		try
			set f to make new folder at desktop with properties {name:"Exported PlayLists"}
		on error
			set f to folder "Exported Playlists" of desktop
		end try
	end tell
	return f
end makeDestinationRootFolder

 
Now let’s say that this script exported the “playlist 2” playlist from Music.app, and you now want to import it into Swinsian.app:
 

set aLocation to path to desktop folder
set aPlaylist to ((aLocation as text) & "Exported Playlists:playlist 2") as alias

tell application "Finder" to set selectedFiles to (files of aPlaylist) as alias list

tell application "Swinsian"
	repeat with selectedFile in selectedFiles
		open (selectedFile as text)
		delay 2
	end repeat
	play -- optional
end tell

 

If I understand the OP correctly, the problem is not to export the playlist from Music, but to import it into Swinsian. Since this software is scriptable, it might be possible to use open with the XML playlist file. Otherwise, there seems to exist no special command to read a playlist.

Yes, unfortunately such a command is not provided in Swinsian.app. There is only an option to do it manually or using GUI scripting.

Now it is clear. You should have shown your script from the very beginning. Then it would be clear to everyone that you need GUI scripting.

Use the following command to change location:

 

tell application "System Events"
	keystroke "g" using {command down, shift down}
end tell

 

Then keystroke posix path you need & return

Thanks again for the speedy reply. Unfortunately, that doesn’t work. Here’s my script:

set theLocation to (path to home folder as string) & "DropBox:Music:"
set posixLocation to POSIX path of theLocation
tell application "Swinsian" to activate
tell application "System Events"
	tell process "Swinsian"
		click menu item "Import iTunes Library…" of menu "File" of menu bar 1
		keystroke "g" using {command down, shift down}
		delay 1
		keystroke posixLocation
	end tell
end tell

I get a “Go To:” window, but to doesn’t enter the folder path in the blank space. Is there any other besides GUI scripting to change the directory in the Open Save dialog? Thanks.

 

set theLocation to (path to home folder as string) & "DropBox:Music:"
set posixLocation to POSIX path of theLocation
tell application "Swinsian" to activate
tell application "System Events"
	tell process "Swinsian"
		click menu item "Import iTunes Library…" of menu "File" of menu bar 1
		delay 1 -- ADDED
		keystroke "g" using {command down, shift down}
		delay 1
		keystroke (posixLocation & return) -- EDITED
	end tell
end tell

 
try little bigger delay values as well

Rather than working with static delays I recommend to wait for appearance of both the Open dialog and the sheet. It is reliable and faster.

Please change the string literal in the first line to the actual name of the Open dialog window

set importWindowName to "Open"
set posixLocation to POSIX path of (path to home folder) & "DropBox/Music/"

tell application "Swinsian" to activate
tell application "System Events"
	tell process "Swinsian"
		click menu item "Import iTunes Library…" of menu "File" of menu bar 1
		repeat until exists window importWindowName
			delay 0.2
		end repeat
		tell window importWindowName
			keystroke "g" using {command down, shift down}
			repeat until exists sheet 1
				delay 0.2
			end repeat
			set value of text field 1 of sheet 1 to posixLocation
			keystroke return
		end tell
	end tell
end tell
1 Like

Hmmm. Your script doesn’t work for me on Monterey.
The import window can’t be referenced as a window, only as a sheet for me.
Here is the modified version that works for me

set posixLocation to POSIX path of (path to home folder) & "DropBox/Music/"

tell application "Swinsian" to activate
tell application "System Events"
	tell process "Swinsian"
		repeat until exists window 1
			delay 0.2
		end repeat
		click menu item "Import iTunes Library…" of menu "File" of menu bar 1
		repeat until exists sheet 1 of window 1 whose description is "open"
			delay 0.2
		end repeat
		set importWindowName to sheet 1 of window 1 whose description is "open"
		tell importWindowName
			keystroke "g" using {command down, shift down}
			repeat until exists sheet 1
				delay 0.2
			end repeat
			set value of text field 1 of sheet 1 to posixLocation
			keystroke return
		end tell
	end tell
end tell

Thanks to all. This is ultimately what worked (I’m assuming there are some peculiarities in the way Swinsian works on my system):

set theLocation to (path to home folder as string) & "DropBox:Music:Library.xml"
set posixLocation to POSIX path of theLocation
tell application "Swinsian" to activate
tell application "System Events"
	tell process "Swinsian"
		repeat until exists window 1
			delay 0.2
		end repeat
		click menu item "Import iTunes Library…" of menu "File" of menu bar 1
		repeat until exists (sheet 1 of window 1 whose description is "open")
			delay 0.2
		end repeat
		keystroke "g" using {command down, shift down}
		delay 1
		set value of text field 1 of sheet 1 of sheet 1 of window 1 to posixLocation
		keystroke return
		delay 1
		keystroke return
	end tell
end tell

Many thanks!