How to convert from Finder Object reference to POSIX path
I'm new to AppleScript. I'm super close to getting what I need done, but I've ran across a snag in the middle.
The error I'm getting is Can’t make quoted form of POSIX path of item 1 of {«class docf» \"filename\" of «class cfol» \"foldername\" of «class cfol» \"Desktop\" of «class cfol» \"Username\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\"} into type Unicode text. the problem area is highlighted and notated below. I've looked all around online but can't find what I need.
Thanks in advance for any and all help!
set text item delimiters to "."
tell application "Finder"
set theFilestoChoose to every item of (choose file with prompt "Please select the file(s) you would like to move and rename" with multiple selections allowed) as list
display dialog "Would you like to move these files to an existing folder and then rename them, or create a new folder and then rename them?" buttons {"Move to an existing folder and rename", "Create a new folder and rename"}
if result = {button returned:"Move to an existing folder and rename"} then
set firstnewname to "Aauuttoommaattoorr"
repeat with index from 1 to the count of theFilestoChoose
set theFilesChosenbeingrenamedfirsttime to item index of theFilestoChoose
set filenamecount to text items of (get name of theFilesChosenbeingrenamedfirsttime)
if number of filenamecount is 1 then
set fileextension to ""
else
set fileextension to "." & item -1 of filenamecount
end if
set the name of theFilesChosenbeingrenamedfirsttime to firstnewname & index & fileextension as string
end repeat
log theFilesChosenbeingrenamedfirsttime
set choosingtheplacetomove to choose folder with prompt "Select the folder to move to"
set thechosenfoldersname to name of folder choosingtheplacetomove -- sets the folder name as text
set AppleScript's text item delimiters to {"-"}
set Numberofthemonthatthebeginningofthefoldername to text item 1 of thechosenfoldersname as string -- for later to append the number back on without having to ask again!
set shortenedname to text item 2 of thechosenfoldersname as string
set the name of choosingtheplacetomove to shortenedname as string
set thefolderstemporarynameaslocation to choosingtheplacetomove as string
move theFilestoChoose to folder thefolderstemporarynameaslocation
log theFilestoChoose
set allfilesindestinationfolder to every file in choosingtheplacetomove as alias list --
set aInitials to the text returned of (display dialog "Whose camera were this/these pictures taken on?" default answer "")
set filteredList to my filterList(allfilesindestinationfolder, aInitials) as list
log filteredList
-- everything above this is correct so far and works perfect
--TROUBLE SECTION BELOW
set theSortedfilterList to (sort filteredList by creation date) -- something happens here with the theSortedfilterList that makes it unintelligible to convert to POSIX later. but I need the files in the order that this line puts them.
log theSortedfilterList
set timetorenamelasttime to theSortedfilterList
set newbasename to shortenedname
repeat with index from 1 to the count of timetorenamelasttime
set theonefile to item index of timetorenamelasttime
set theonefilenamecount to text items of (get name of theonetwothreefile)
if number of theonefilenamecount is 1 then
set fileextensionone to ""
else
set fileextensionone to "." & item -1 of theonefilenamecount
end if
tell application "System Events" to set CreaDate to creation date of file theonefile
set CreaDate2 to CreaDate as text -- need to trim down to the first 10 characters and eliminate the "-"
set AppleScript's text item delimiters to {""}
set shorteneddatename to text items 1 thru 10 of CreaDate2 as string
set the name of theonefile to shorteneddatename & {"-"} & newbasename & {"-"} & aInitials & {"-"} & index & fileextensionone as string
end repeat
--Trouble section above
set the name of choosingtheplacetomove to Numberofthemonthatthebeginningofthefoldername & "-" & shortenedname as string -- returns the month prefix to the foldername
else if result = {button returned:"Create a new folder and rename"} then
set repeatConfirmation to true --Boolean to decided if script should be repeated; default is to repeat
repeat while (repeatConfirmation = true) --Repeat if Any Tests Are Failed
set thefirstquestion to choose from list {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} with title "Event Month Selection" with prompt "Select the month in which the event started. Select only one month:"
set theMonthAnswer to result
if theMonthAnswer = false then --"For historical reasons, choose from list is the only dialog command that returns a result (false) instead of signaling an error when the user presses the “Cancel” button."
set repeatConfirmation to false
set exitingeventmonth to display dialog ("You are exiting selecting the event month phase of the program. You will need to manually fix this decision") buttons {"OK"} with title "Exiting Event Month Selection"
if button returned of exitingeventmonth = "OK" then
set theMonthAnswer to ""
end if
else
set confirmationanswer to display dialog "You selected " & theMonthAnswer & ", is this correct? " buttons {"Yes, that's correct.", "No, the picture(s) are from a different month."} with title "Confirm Event Month Selection"
if button returned of confirmationanswer = "Yes, that's correct." then
set repeatConfirmation to false
else
display dialog ("That's okay, you can select another again!") buttons {"OK"} with title "Return to Event Month Selection"
end if
end if
end repeat
log theMonthAnswer
if theMonthAnswer = "" then
set monthtonumber to "You have cancelled this action."
log monthtonumber
else if theMonthAnswer = {"January"} then
set monthtonumber to "01"
log monthtonumber
else if theMonthAnswer = {"February"} then
set monthtonumber to "02"
log monthtonumber
else if theMonthAnswer = {"March"} then
set monthtonumber to "03"
log monthtonumber
else if theMonthAnswer = {"April"} then
set monthtonumber to "04"
log monthtonumber
else if theMonthAnswer = {"May"} then
set monthtonumber to "05"
log monthtonumber
else if theMonthAnswer = {"June"} then
set monthtonumber to "06"
log monthtonumber
else if theMonthAnswer = {"July"} then
set monthtonumber to "07"
log monthtonumber
else if theMonthAnswer = {"August"} then
set monthtonumber to "08"
log monthtonumber
else if theMonthAnswer = {"September"} then
set monthtonumber to "09"
log monthtonumber
else if theMonthAnswer = {"October"} then
set monthtonumber to "10"
log monthtonumber
else if theMonthAnswer = {"November"} then
set monthtonumber to "11"
log monthtonumber
else if theMonthAnswer = {"December"} then
set monthtonumber to "12"
log monthtonumber
end if
set theNameofFoldertoMake to text returned of (display dialog "Please enter the name of the new folder you are creating:" default answer "" with title "New Folder Name")
set LocationOfNewFolder to choose folder with prompt "Choose the location of the new folder you are creating:"
set theNewNameofFoldertoMake to monthtonumber & "-" & theNameofFoldertoMake
set newfolderaction to make new folder at LocationOfNewFolder with properties {name:theNewNameofFoldertoMake}
move theFilestoChoose to newfolderaction
end if
end tell
--function
on filterList(allfilesindestinationfolder, aInitials)
set patterns to {aInitials as string, "Aauuttoommaattoorr"}
set output to {}
repeat with aFile in the allfilesindestinationfolder
repeat with aPattern in patterns
set filepath to aFile as string
if filepath contains aPattern then
set end of the output to aFile
exit repeat
end if
end repeat
end repeat
return output
end filterList
MacBook Pro (13-inch Mid 2012), OS X Mavericks (10.9.5)