Getting a list of filenames from a list of aliases

Hi there,

Could you please help me.

The Script below does what I require it to do, however I would like to take the variable FileIn and create a list of file names to then make into a string with a [space]&[space] between each one to use in another process. But for the life of me I can’t figure out how the get a list of file names from the aliases.

I have got this far with a lot of googling and searching this forum.

Could you please help.

Cheers
Andy

--Gets the job number
set userReplyJobNumber to display dialog "Job number:" default answer "" buttons {"Cancel", "OK"} default button 2
set JobNumber to text returned of userReplyJobNumber

--Set up default job location
tell application "Finder"
	set JobFolderHome to "Macintosh HD:Volumes:Digital Operations:CTP:1 Work in Progress:"
	--set JobFolder to (first folder of folder JobFolderHome whose name begins with myJobNumber)
	set JobFolderLocation to (first folder of folder JobFolderHome whose name begins with JobNumber)
	set JobFolderLocationText to JobFolderLocation as text
	set JobFolderLocationAlias to JobFolderLocation as alias
end tell

--Select the PDF to version
set FileIn to (choose file with prompt "Select PDF to have version naming" default location JobFolderLocationAlias with multiple selections allowed)

--Set the new name and locations
set UserReplyVersionName to display dialog "Version Name:" default answer "" buttons {"Cancel", "OK"} default button 2
set VersionName to text returned of UserReplyVersionName

set PersoVersionDestination to JobFolderLocationText & "Stuff for DPL:PDFs With Text"
set PersoVersionDestinationAlias to PersoVersionDestination as alias
set PrintVersionDestination to JobFolderLocationText & "Documents"
set PrintVersionDestinationAlias to PrintVersionDestination as alias

--Duplicate files and rename
tell application "Finder"
	
	
	if (count of FileIn) = 1 then
		set this_file to (duplicate FileIn to PrintVersionDestinationAlias with replacing) as alias
		set {name:Nm, name extension:Ex} to this_file
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set name of this_file to JobNumber & "_" & VersionName & "." & Ex
		
		set this_file2 to (duplicate FileIn to PersoVersionDestinationAlias with replacing)
		set {name:Nm2, name extension:Ex2} to this_file2
		if Ex2 is missing value then set Ex2 to ""
		if Ex2 is not "" then set Nm2 to text 1 thru ((count Nm2) - (count Ex2) - 1) of Nm2
		set name of this_file2 to JobNumber & "_" & VersionName & " Perso" & "." & Ex2
	else
		repeat with i from 1 to count of FileIn
			--set i to item _i of FileIn
			set theFile to (item i of FileIn as alias)
			set this_file to (duplicate theFile to PrintVersionDestinationAlias with replacing) as alias
			set {name:Nm, name extension:Ex} to this_file
			if Ex is missing value then set Ex to ""
			if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
			set name of this_file to JobNumber & "_" & VersionName & "_" & i & "." & Ex
			
			set this_file2 to (duplicate theFile to PersoVersionDestinationAlias with replacing)
			set {name:Nm2, name extension:Ex2} to this_file2
			if Ex2 is missing value then set Ex2 to ""
			if Ex2 is not "" then set Nm2 to text 1 thru ((count Nm2) - (count Ex2) - 1) of Nm2
			set name of this_file2 to JobNumber & "_" & VersionName & "_" & i & " Perso" & "." & Ex2
		end repeat
	end if
	
end tell

Hello.

In order to get a filename from an alias, you first coerce the aiias to text, then you have a hfs path, consisting of several “:” separated items, where the last one is the filename, or a colon, if it was a folder name.
To get the filenames, we then save the old text item delimiter, and apply “:” as one, then we take the filename, and breaks it into a list, before we take the last one.

tell application "Finder"
	if selection = {} then
		beep
		return
	end if
	set n to (count selection as list)
	if n > 1 then
		beep
		return
	else
		set a to (selection as alias) as text
	end if
end tell
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set b to last item of text items of a
set AppleScript's text item delimiters to tids

if you wanted to build a long string of filenames, you might have one variable. you build up in a loop with something like this:

# assuming a list of filenames
set a to ""
repeat with i from 1 to (count filenames)
	if i = 1 then
		set a to item i of filenames
	else
		set a to a & space & item i of filenames
	end if
end repeat

Hi,

try this, the variable fileNameList contains the (original) file names separated by a space character.


--Gets the job number
set JobNumber to text returned of (display dialog "Job number:" default answer "" buttons {"Cancel", "OK"} default button 2)

--Set up default job location

set JobFolderHome to "Digital Operations:CTP:1 Work in Progress:" -- HFS paths are supposed to start directly with the disk name
--set JobFolder to (first folder of folder JobFolderHome whose name begins with myJobNumber)
try
	tell application "Finder" to set JobFolderLocationText to (first folder of folder JobFolderHome whose name begins with JobNumber) as text
on error
	display dialog "No job folder found" buttons {"Cancel"} default button 1
end try

--Select the PDF to version
set FileIn to (choose file with prompt "Select PDF to have version naming" default location alias JobFolderLocationText with multiple selections allowed)

--Set the new name and locations
set VersionName to text returned of (display dialog "Version Name:" default answer "" buttons {"Cancel", "OK"} default button 2)

set PersoVersionDestination to JobFolderLocationText & "Stuff for DPL:PDFs With Text:"
set PrintVersionDestination to JobFolderLocationText & "Documents:"

--Duplicate files and rename

set countFileIn to count FileIn
set fileNameList to {}
repeat with i from 1 to countFileIn
	--set i to item _i of FileIn
	if countFileIn = 1 then
		set idx to ""
	else
		set idx to "_" & i
	end if
	set theFile to (item i of FileIn)
	tell application "Finder"
		set this_file to (duplicate theFile to folder PrintVersionDestination with replacing) as alias
		set nameExtension to my stripExtension(get name extension of theFile)
		set end of fileNameList to name of theFile
		set name of this_file to JobNumber & "_" & VersionName & idx & nameExtension
		
		set this_file2 to (duplicate theFile to folder PersoVersionDestination with replacing) as alias
		set name of this_file2 to JobNumber & "_" & VersionName & idx & " Perso" & nameExtension
	end tell
end repeat

set {TID, text item delimiters} to {text item delimiters, space}
set fileNameList to fileNameList as text
set text item delimiters to TID
display dialog fileNameList buttons {"Cancel", "OK"} default button "OK"


on stripExtension(Ex)
	if Ex is missing value or Ex is "" then
		return ""
	else
		return "." & Ex
	end if
end stripExtension

Hi there,

Thanks for you quick reply.

I keep on getting an error with the number -10004 when I try and use the first bit of script.

I have also tried as it is just selecting the file.

set FileIn to {alias "Digital Operations:CTP:1 Work in Progress:83777:Customer Documents:2013-05-03  11-52-00:Test1.pdf", alias "Digital Operations:CTP:1 Work in Progress:83777:Customer Documents:2013-05-03  11-52-00:Test2.pdf"}

tell application "Finder"
	if FileIn = {} then
		beep
		return
	end if
	set n to (count FileIn as list)
	if n > 1 then
		beep
		return
	else
		set a to (FileIn as alias) as text
	end if
end tell
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set b to last item of text items of a
set AppleScript's text item delimiters to tids

below is what is in the replies screen:

tell application “Finder”
beep
→ error number -10004
end tell
tell current application
beep
end tell

Cheers
Andy

Hi StefanK,

I get an error with yours as well. This is the replies from AppleScript Editor:

tell application “AppleScript Editor”
display dialog “Job number:” default answer “” buttons {“Cancel”, “OK”} default button 2
→ {text returned:“TEST”, button returned:“OK”}
end tell
tell application “Finder”
get folder 1 of folder “Digital Operations:CTP:1 Work in Progress:” whose name starts with “TEST”
→ “Digital Operations:CTP:1 Work in Progress:TEST-ARW-Test Job:”
end tell
tell application “AppleScript Editor”
choose file with prompt “Select PDF to have version naming” default location alias “Digital Operations:CTP:1 Work in Progress:TEST-ARW-Test Job:” with multiple selections allowed
→ {alias “Digital Operations:CTP:1 Work in Progress:TEST-ARW-Test Job:Customer Documents:2013-05-03 11-52-00:NVUVA - GI - 10pp Booklet_56754_HR.pdf”, alias “Digital Operations:CTP:1 Work in Progress:TEST-ARW-Test Job:Customer Documents:2013-05-03 11-52-00:NVUVB - GI - 10pp Booklet_56754_HR.pdf”}
display dialog “Version Name:” default answer “” buttons {“Cancel”, “OK”} default button 2
→ {text returned:“test”, button returned:“OK”}
end tell
tell application “Finder”
copy alias “Digital Operations:CTP:1 Work in Progress:TEST-ARW-Test Job:Customer Documents:2013-05-03 11-52-00:NVUVA - GI - 10pp Booklet_56754_HR.pdf” to folder “Digital Operations:CTP:1 Work in Progress:TEST-ARW-Test Job:Documents:”
→ document file “NVUVA - GI - 10pp Booklet_56754_HR.pdf”
→ error number 0
get document file “NVUVA - GI - 10pp Booklet_56754_HR.pdf”
→ error number -1728 from document file “NVUVA - GI - 10pp Booklet_56754_HR.pdf”
Result:
error “Can’t make «class docf» "NVUVA - GI - 10pp Booklet_56754_HR.pdf" of application "Finder" into type alias.” number -1700 from «class docf» “NVUVA - GI - 10pp Booklet_56754_HR.pdf” to alias

Yours does look neater than mine and I was getting this error randomly on my script as well.

Any ideas?

Cheers
Andy

the alias coercion is actually not necessary


.
set this_file to (duplicate theFile to folder PrintVersionDestination with replacing)
.
set this_file2 to (duplicate theFile to folder PersoVersionDestination with replacing)
.

Hello.

I just corrected my example slightly to suit your needs better. I have embellished double-ticks around the filenames so you don’t get into trouble when you want to work further on the filenames in the string, should you have spaces in your filenames.

set templist to ""
set outList to ""
tell application "Finder"
	set FileIn to every item of (get selection as list) as alias list
	# my line above for testing that it works -- just remove it.
	set n to (count FileIn)
	repeat with i from 1 to n
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
		set aFile to last item of text items of (((item i of FileIn) as alias) as text)
		set AppleScript's text item delimiters to tids
		if i = 1 then
			set outList to "\"" & aFile & "\""
		else
			set outList to outList & space & "\"" & aFile & "\""
		end if
	end repeat
	
end tell

if you’re in a Finder tell block anyway, you can retrieve the file name directly and without any coercions


set outList to {}

tell application "Finder" to set FileIn to (get selection) -- selection is always a list
if FileIn is {} then return
repeat with i from 1 to (count FileIn)
	tell application "Finder" to set end of outList to quote & name of item i of FileIn & quote
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
set outList to outList as text
set text item delimiters to TID
outList


If I understand well, you are trying to get the full pathname of the file whose name is “NVUVA - GI - 10pp Booklet_56754_HR.pdf”.
The error message claims that being unable to guess the path to the folder containing the file, the Finder is unable to build the asked pathname.

KOENIG Yvan (VALLAURIS, France) jeudi 16 mai 2013 18:10:59

Hello. :slight_smile:

I know, but I looked at the OP’s code.

Hi there,

Thanks for all your replies and help. It seams to be working now so on to the next bit.

Thanks
Andy