Converting a Folder of Audio Files to Different Format

Ok, ok, I have finally taken the plunge to “try” learn AppleScript in my retirement years. And, this puts me in the NOOB category, thus, please be a little patient with me. I think I had a little success in my first venture, yet, I am seeking some feedback/guidance as it relates to the script’s effectiveness/efficiency.

Description of script tasks:
I was performing manual tasks of converting audio files from *.wav to *.rx2 file type to be able to use these in other applications requiring the REX2 format. I have been using PropellerHead’s Recycle 2 (very dated package) software to do so. I developed this script to enable the file selections (Folder B) and auto-convert & save to another folder (Folder C). Both these folders are located within Folder A.

The script currently does what it is intended for based on how ReCycle 2 was setup. However, I am not able to specifically point the ReCycle to a specific folder and/or file, thus, I need some inputs in this area.

The script currently goes to the Folder B (wav files location) and counts the number of files within the folder. Then uses such “count” to iterate through a Repeat loop to pick a file (using a pinter within the app’s file finder), almost like a pointer. So, it pick file 1 through X. Recycle then grabs the file, and proceeds to store it in Folder C (rx2 files location).

Here is the current script:


tell application "Finder"
	
	set theFolder to (POSIX file "/Users/myname/Documents/MusicAppFolder/wavFiles_to_rx2Files/wavFiles/") as text
	set audioFilesType to "wav" as text
	set NumOfFiles to count of (files in folder theFolder whose name extension is audioFilesType)
	set filesList to every file in folder theFolder whose name extension is audioFilesType
	
	repeat with i from 1 to NumOfFiles
		
		set openFile to item i of filesList as text
		
		tell application "ReCycle" to activate
		tell application "System Events"
			
			key code 13 using command down -- Close current document. [W]
			delay 1
			key code 31 using command down -- Open an audio file or a ReCycle file. [O]
			delay 1
			-- key code 48 -- Position cursor for file selection. [TAB]
			-- delay 1
			repeat i times
				key code 125 -- Select 1st file in Window using Arrrow Down
				delay 1
			end repeat
			-- key	stroke openFile -- Specifies audio file to select.
			-- delay 1
			key code 36 -- Return [Return]
			delay 1
			key code 1 using command down -- Save current document as a ReCycle file. [S]
			delay 1
			repeat 3 times -- ReCycle application requires 3 Tabs to get to Save selection
				key code 48
				delay 1
			end repeat
			key code 36 -- Return
			key code 13 using command down -- Close current document.
			delay 1
			key code 12 using command down -- Quit ReCycle. [Q]
			delay 1
			
		end tell
		
	end repeat
	
end tell

Challenges with script:
(1) I would prefer to have the ability to specify the Folder location and File name of the files being converted, instead of the current “pointer” using the Count. The Count’s problem is that if there are other files within that same directory, then, it could potentially grab the incorrect file and crash.
(2) In addition, I would like to specify the Folder location so I can use other folders as well. I tried using the List of Files array, yet, was not successful.

Anyway, it would be great if I could get some feedback on these challenges and/or the script I wrote. I am sure there are much better ways (or different ways) of doing such. Thank you!

p.s. Does one have the ability to use Finder during the execution of this script? Is Finder a “hostage” during such script routines?

You should be able to
Tell “RX” to open with fileList

I

Also you can set your fileCount to The the count of filesList instead.

In your repeat loop:

You want you openFile to not be coerced to text. You want alias.

Or you can
Repeat with ipenFile in filesList

1 Like

Thank you very much for your response, much appreciated! I will give that a go tomorrow morning and see how it goes. Will certainly report back on my results/test.

Again, thanks!

If you mean, can you do other stuff while the script is running… not really. When using UI scripting, the script will blindly type the key codes and do whatever, regardless of which window is in front or what dialogue is open.

But… the finder can play a role in such a project. If the files default to opening in ReCycle — eg they would open in it with a double-click — then you could open them from the finder. This script will cycle through the selected files in the finder and open them.

tell application "Finder"
	set cf to target of Finder window 1 as alias
	set salt to selection as alias list
	
	repeat with sel in salt
		open sel
		-- process each file
	end repeat
	tell application "ReCycle" to activate
end tell

NB While this method should work fine, it would likely require some tweaking depending upon how recycle works. However, I wouldn’t suggest it if the recycle app has any applescript functionality. If it can open files then technomorph’s ideas would be more useful.

1 Like

Before I respond with an update on my test results, I would like to provide you with a quick overview of what the program ReCycle does through my tasks process without the AppleScript.

When you run the application you will get this window:

The initial Folder location defaults to the last known folder you used to open a file.
With mouse/pad:
One can select another folder if desire to do so.
Place cursor in the Search box.
Click to select a file in the files location window.
Click on Open, or,
Cancel to close window.
With key strokes:
TAB to select Search box.
Down arrow key to go into files location window and scroll to any file within. The “Open” will be highlighted. Once you hit Return the file is opened.
Using TAB will jump between files location area and Search box.

Once a file is selected, the following window is displayed.

Within this window you can certainly perform many tasks with the file, yet in this instance, I just want to save the file which was just imported as a *.wav to a *.rx2, thus, I use the mouse/pad to use the File menu and perform a Save (or, with keys, CMD+S).

Now, this Save window is displayed:

The filename is displayed as the imported file, and, the extension is now (by default) set to *.rx2, which is what I need.
The Folder location defaults to the last location used for a Save file.
One can decide to change the save location if desired.

=================================================================

** When using the AppleScript, I would like the ability to:

  1. Specify the Open File location,
  2. be able to “tell” ReCycle the file I want it to process, then,
  3. specify the Save File location.

When I try to use the following:

tell application "ReCycle" to activate (open with filesList)

I get: “error “ReCycle got an error: Can’t continue open.” number -1708” in the Result box.

The above change worked great. Implemented!

Did not fully follow what this statement (“You want you openFile to not be coerced to text. You want alias.”) is suggesting I do/or change!!

When I try the following:

repeat with ipenFile in filesList
		
		set openFile to item ipenFile

I get the following error:
error “Finder got an error: Can’t make document file… of startup disk into type integer.” number -1700 from document file

Anyway, I will keep trying to debug in the meantime. If you have any inputs please let me know.

Thank you for the support!

Yes, that is what I meant. I noticed that when the script was running, and, I tried opening a Finder window and/or trying to perform other tasks, it seemed the script did not like that at all.

Thanks for the script! Unfortunately, the files DO NOT default opening in ReCycle.

What do you refer too when saying “if the ReCycle app has any AppleScript functionality”?
The app can open files, yet, not sure it can open files via script commands using the folders/filenames!!! Still testing.

Thank you for all the ideas, much appreciated! Certainly learning a bunch already!

Sorry hat should be
openFile in fileList

openFile is already site as
The next item in fileList so no need to
Use set openFile to item i of openFiles

You want to tellApp to open files
Then tellApp to activate

Your sending an illegal command

1 Like

Here is the current script under the “debugging” process…

tell application "Finder"
	
	set theFolder to (POSIX file "/Users/luistorres/Documents/Spectrasonics/wavFiles_to_rx2Files/wavFiles/") as text
	set audioFilesType to "wav" as text
	set filesList to every file in folder theFolder whose name extension is audioFilesType
	set filesCount to count of filesList
	
	
	-- repeat with i from 1 to filesCount
	repeat with ipenFile in filesList
		
		set openFile to item ipenFile
		-- set openFile to item i of filesList
		-- set openFile to item i of filesList as text
		
		tell application "ReCycle" to activate
		-- tell application "ReCycle" to activate (open with filesList)
		tell application "System Events"
			
			-- key code 13 using command down -- Close current document. [W]
			-- delay 1
			-- key code 31 using command down -- Open an audio file or a ReCycle file. [O]
			-- delay 1
			-- repeat i times
			-- key code 125 -- Select 1st file in Window using Arrrow Down
			-- delay 1
			-- end repeat
			-- key code 36 -- Return [Return]
			-- delay 1
			key code 1 using command down -- Save current document as a ReCycle file. [S]
			delay 1
			repeat 3 times -- ReCycle application requires 3 Tabs to get to Save selection
				key code 48
				delay 1
			end repeat
			key code 36 -- Return
			key code 13 using command down -- Close current document.
			delay 1
			key code 12 using command down -- Quit ReCycle. [Q]
			delay 1
			
		end tell
		
	end repeat
	-- end repeat
	
end tell

Thanks for the inputs. I should have caught that.

So, just to make sure, I can tellApp to open the files, prior to actually the app being activated/opened?

Again, thanks!

Will keep testing!

tell application "ReCycle" to open filesList
		tell application "ReCycle" to activate

Result: error “Can’t make «class docf»… of application "Finder" into the expected type.” number -1700 from «class docf»

In Script Editor, type command-shift-O (or File > Open Dictionary…).

An ‘open dictionary’ window will open. Scroll down and see whether there is an entry for recycle. If so, open it. The resulting document would contain the commands available to the app.

I looked on their website but there isn’t a lot of information there.

1 Like

I had checked on that earlier today, yet, the ReCycle app does not show up as one of the supported apps within the Dictionary. Unfortunately, this software package is “somewhat” (better said REALLY) dated , and, the developer pretty much is on a “hands-off” mode .

Some good news to report…

I am now able to ensure that the correct folders are used for both opening and saving the converted files. This is done by using the shortcut key command (Shift-CMD-G) while on the Recycle’s app open or save windows. It basically opens a Go to Folder window where I can provide the keystrokes (or folder alias [I guess!!]) for the required folder.

Now, the missing link is to figure out how to tell ReCycle’s app which file to pick from the shown window. So, I need to figure out which commands will enable such, and that the app will accept as input.

Dunno if this will work but it might make things simple. It takes the selected files in the finder and makes a list of those with the specified extension. It then uses the shell to open them in the specified application.

tell application "Finder"
	set salt to selection as alias list
	set wavList to {}
	repeat with sel in salt -- generate list of paths for each wav file
		if name extension of sel is "wav" then -- if extension is .wav then add to list
			set end of wavList to quoted form of POSIX path of contents of sel
		end if
	end repeat
end tell


set fissile to "/Applications/ReCycle.app" -- or whatever the path to the application is
repeat with wav in wavList
	do shell script "open -a " & fissile & space & wav
	delay 1.2 -- adjust according to your computer's speed
	
	-- processing code here
	
end repeat

-- NB path to app can be acquired with this command…
-- POSIX path of (path to application "ReCycle")
1 Like

Thanks for the work on this script, much appreciated!

So, I added the “processing code” to what you outlined in the script above. However, I am missing how to link the folder locations to to the beginning of your code. Please refer to the overall script below (I added code at 2 spots between “start of added code” and “end of added code”):



tell application "Finder"
	
	-- Start of added code
	
	set theOpenFilesFolder to (POSIX file "/Users/Path1/wavFiles/") as text
	set theSaveFilesFolder to (POSIX file "/Users/Path2/rx2Files/") as text
         
    -- ???
	set sel to every file in folder theOpenFilesFolder
	-- ???
	
    -- End of added code
	
	
	set salt to selection as alias list
	set wavList to {}
	repeat with sel in salt -- generate list of paths for each wav file
		if name extension of sel is "wav" then -- if extension is .wav then add to list
			set end of wavList to quoted form of POSIX path of contents of sel
		end if
	end repeat
end tell


set fissile to "/Applications/ReCycle/ReCycle.app" -- or whatever the path to the application is
repeat with wav in wavList
	do shell script "open -a " & fissile & space & wav
	delay 1.2 -- adjust according to your computer's speed
	
	
	-- Start of added code
	
	tell application "System Events"
		
		key code 1 using command down -- Save current document as a ReCycle file. [S]
		delay 1
		key code 5 using {shift down, command down}
		keystroke theSaveFilesFolder as text
		delay
		key code 36
		delay 1
		repeat 3 times -- ReCycle application requires 3 Tabs to get to Save selection
			key code 48
			delay 1
		end repeat
		key code 36 -- Return
		key code 13 using command down -- Close current document.
		delay 1
		key code 12 using command down -- Quit ReCycle. [Q]
		delay 1
		
	end tell
	
	-- End of added code
	
	
end repeat

I need to learn about the line “set salt to selection as alias list” and how to connect.

Thank you!

Made some additional progress…modified in order to connect the selection to my “theOpenFilesFolder” (POSIX).



tell application "Finder"
	
	-- Start of added code
	
	set theOpenFilesFolder to (POSIX file "/Users/Path1/wavFiles/") as text
	set theSaveFilesFolder to (POSIX file "/Users/Path2/rx2Files/") as text
	
	-- End of added code
	
	
	set salt to (every item of folder theOpenFilesFolder) as alias list
	-- set salt to selection as alias list
	set wavList to {}
	repeat with sel in salt -- generate list of paths for each wav file
		if name extension of sel is "wav" then -- if extension is .wav then add to list
			set end of wavList to quoted form of POSIX path of contents of sel
		end if
	end repeat
end tell


set fissile to "/Applications/ReCycle/ReCycle.app" -- or whatever the path to the application is
repeat with wav in wavList
	do shell script "open -a " & fissile & space & wav
	delay 1.2 -- adjust according to your computer's speed
	
	
	-- Start of added code
	
	tell application "System Events"
		
		key code 1 using command down -- Save current document as a ReCycle file. [S]
		delay 1
		key code 5 using {shift down, command down}
		keystroke theSaveFilesFolder as text
		delay
		key code 36
		delay 1
		repeat 3 times -- ReCycle application requires 3 Tabs to get to Save selection
			key code 48
			delay 1
		end repeat
		key code 36 -- Return
		key code 13 using command down -- Close current document.
		delay 1
		key code 12 using command down -- Quit ReCycle. [Q]
		delay 1
		
	end tell
	
	-- End of added code
	
	
end repeat


Processing, yet, some issues of windows, could be timing of events.

So, the script is “sort of” working. It is opening the file (from correct folder), then, proceeds to save it in the correct location/folder.

However, it is assigning the overall path of the file as the filename itself - for every single converted file.

Then, of course, on each cycle it freezes asking to either cancel (or Replace file).

GREAT news… It is finally working the way that I expected!

First of all, I want to thank both technomorph and Mockman for all the feedback to get this to work. I really appreciate your support!

Special thanks to Mockman for the final “shell” script.

It actually forced me also to get some additional learnings in my 3 to 4 days worth of learning AppleScript!

Anyway, the script now takes 2min 10sec to convert 14 audio files in format (from wav to rx2). My previous script, which was using a pointer and was not as reliable (not even knowing the folders needed for selection), ran for 4min 12sec. So we got about a 50% reduction in time, this is awesome.

And, the current script needs to be improved with all the delays I placed in the core file processing area. For example, the script is keystroking in the folder location for the converted files every cycle (which takes a chunk of the time from the overall), yet, I think there should be a way to only let ReCycle know once!!!

Here is the working (without the potential productivity improvements) script. Hope it can help someone else in a “similar” situation.

THANKS!



tell application "Finder"
	
	-- Start of added code
	
	set theOpenFilesFolder to (POSIX file "/Users/Path1/wavFiles/") as text
	set theSaveFilesFolder to (POSIX file "/Users/Path2/rx2Files/") as text
	set rx2FilesPath to theSaveFilesFolder as alias

	-- End of added code
	
	
	set salt to (every item of folder theOpenFilesFolder) as alias list
	set wavList to {}
	repeat with sel in salt -- generate list of paths for each wav file
		if name extension of sel is "wav" then -- if extension is .wav then add to list
			set end of wavList to quoted form of POSIX path of contents of sel
		end if
	end repeat
end tell


set fissile to "/Applications/ReCycle/ReCycle.app" -- or whatever the path to the application is
repeat with wav in wavList
	do shell script "open -a " & fissile & space & wav
	delay 1.2 -- adjust according to your computer's speed
	
	
	-- Start of added code
	
	tell application "System Events"
		
		key code 1 using command down -- Save current document as a ReCycle file. [S]
		delay 1
		key code 5 using {shift down, command down}
		delay 1
		keystroke rx2FilesPath
		delay 2
		repeat 2 times
			key code 36
			delay 1
		end repeat
		key code 36 -- Return
		key code 13 using command down -- Close current document.
		delay 1
		key code 12 using command down -- Quit ReCycle. [Q]
		delay 1
		
	end tell
	
	-- End of added code
	
	
end repeat

Is it possible to perform a “do shell script” command to save the files? If so, it could help with the overall efficiency of the run. Thanks!