Cant open file in this script

--This does not open the ".cpr" file
tell application "Finder"
	set x to "Macintosh HD:Users:shawnbrady:Dropbox:Audio:Features:"
	copy folder "Macintosh HD:Users:shawnbrady:Desktop:Features:" to folder x
	delay 1
	
	set y to every item of window 1 whose name contains "omf"
	set omf_name to name of item 1 in y as string
	set file_name to characters 1 thru -5 of omf_name as string
	set folder_name to characters 1 thru -3 of file_name as string
	set name of file "Macintosh HD:Users:shawnbrady:Dropbox:Audio:Features:Features:FEAT.cpr" to file_name & ".cpr"
	set name of folder "Macintosh HD:Users:shawnbrady:Dropbox:Audio:Features:Features" to folder_name & "NPR"
	
	set xx to item 1 of window 1 as string
	set yy to (every item of folder xx whose name contains ".cpr") as string
	delay 1
	open yy
	
	set x to every item in folder 1 of window 1 whose name contains (".cpr" as string)
	set x to x as string
	open x
	
end tell



--This DOES open the .cpr file if in a new empty script
tell application "Finder"
	set x to item 1 of window 1 as string
	set y to (every item of folder x whose name contains ".cpr") as string
	open y
end tell

figured it out

set x to (every item in folder 1 of window 1 whose name contains ".cpr") as string
	set x to x as string
	open x

it worked once, here is another try, this is so frustrating

tell application "Finder"
	
	--copy foler
	set x to "Macintosh HD:Users:shawnbrady:Dropbox:Audio:Features:"
	copy folder "Macintosh HD:Users:shawnbrady:Desktop:Features:" to folder x
	delay 1
	
	--rename folder and file
	set y to every item of window 1 whose name contains "omf"
	set omf_name to name of item 1 in y as string
	set file_name to characters 1 thru -5 of omf_name as string
	set folder_name to characters 1 thru -3 of file_name as string
	set name of file "Macintosh HD:Users:shawnbrady:Dropbox:Audio:Features:Features:FEAT.cpr" to file_name & ".cpr"
	set name of folder "Macintosh HD:Users:shawnbrady:Dropbox:Audio:Features:Features" to folder_name & "NPR"
	
	-- open file
	set xx to every folder of window 1 as string
	set yy to (every item of folder xx whose name contains ".cpr") as string
	delay 1
	open file yy
end tell

Some random suggestions…

In general, the contents (or target) of a window are transient. It’s easy to click somewhere or even to have the script make changes. So try and work with the target of the window instead. If you know the folder that you’re working with in advance, then work directly with it and forego using the window at all (except to observe).

If you get something that is plural (e.g. files of, folders of, items of) then the result will be a list — even when there is only a single result. This can affect how you work with stuff later on.

If you are only interested in what you know to be the first item then work directly with that. For example:

set omfName to name of (item 1 of (get files of ww whose name contains "dolphins"))

Some items are inherently strings (or text) so there is no need or point in coercing them to text. A file’s name is an example. If you look up file in the Finder’s dictionary, you will see that the name property is ‘text’.

Characters and text are similar but not identical. A character is a single item. Multiple characters are a list of such items. A text with multiple letters is a string. You can use text and then leave out the step of coercing the characters to text.

set x to "abcde"

set c to characters of x
--> {"a", "b", "c", "d", "e"}
set c3 to characters -3 thru -1 of x
--> {"c", "d", "e"}

set t to text of x
--> "abcde"
set t3 to text -3 thru -1 of x
--> "cde"

Be careful when coercing a file reference to text (e.g. set xx to every folder of window 1 as string). If you have multiple results then all of those items will be concatenated into a single string — which needless to say, won’t be useful when you then try to use it as a file object.

Finally, you can open a list of files but you can’t open a string of them. Comment out the last two lines and run your script. Look at the result of ‘yy’.

2 Likes