Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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)

Posted on Mar 5, 2015 10:13 AM

Reply
9 replies

Mar 5, 2015 12:33 PM in response to tc8213

This will give you some hints:

(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles

For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run

For running shell commands see:

http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html



*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath

set unixDesktopPath to POSIX path of desktopPath

log "unixDesktopPath = " & unixDesktopPath

set quotedUnixDesktopPath to quoted form of unixDesktopPath

log "quoted form is " & quotedUnixDesktopPath

try

set fromUnix to do shell script "ls -l " & quotedUnixDesktopPath

display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix

on error errMsg

log "ls -l error..." & errMsg

end try

end run

Mar 5, 2015 12:59 PM in response to tc8213

Good examples of HFS to POSIX path to back to HFS via AppleScript.


You will find yourself using many log statements.😉


Generally, you can find answers to a brief question with a browser search

{MacScripter, AppleScript, MacTech} (one of these) followed by a short list of keywords that form your question

  1. Satimage POSIX file returns as the very first search item the link above.
  2. MacScripter POSIX file path is the very first retrieved link
  3. AppleScript POSIX file path returns the Satimage link as the first retrieval and a Wiki entry as second.
  4. AppleScript Language will retrieve the link to the PDF AppleScript Language Guide as a second item


Some of the older posts will use something as string, whereas the current usage is something as text.

Mar 5, 2015 5:57 PM in response to tc8213

Okay, so you wanted the debugging and error messages. Thank you for the coaching. Here is what I get from the following line:

set theSortedfilterList to (sort filteredList by creation date)

log theSortedfilterList


Log returns:

(*document file Aauuttoommaattoorr3.AVI of folder SnowDay of folder Desktop of folder Username of folder Users of startup disk, document file Aauuttoommaattoorr2.MOV of folder SnowDay of folder Desktop of folder Username of folder Users of startup disk, document file Aauuttoommaattoorr1.mov of folder SnowDay of folder Desktop of folder Username of folder Users of startup disk*)

set pxFile to POSIX path of ((theSortedfilterList) as alias)



Error: "Can’t make {«class docf» \"Aauuttoommaattoorr3.AVI\" of «class cfol» \"SnowDay\" of «class cfol» \"Desktop\" of «class cfol» \"Username\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\", «class docf» \"Aauuttoommaattoorr2.MOV\" of «class cfol» \"SnowDay\" of «class cfol» \"Desktop\" of «class cfol» \" Username \" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\", «class docf» \"Aauuttoommaattoorr1.mov\" of «class cfol» \"SnowDay\" of «class cfol» \"Desktop\" of «class cfol» \" Username \" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\"} into type alias." number -1700 from {«class docf» "Aauuttoommaattoorr3.AVI" of «class cfol» "SnowDay" of «class cfol» "Desktop" of «class cfol» "Username" of «class cfol» "Users" of «class sdsk», «class docf» "Aauuttoommaattoorr2.MOV" of «class cfol» "SnowDay" of «class cfol» "Desktop" of «class cfol» "Username" of «class cfol» "Users" of «class sdsk», «class docf» "Aauuttoommaattoorr1.mov" of «class cfol» "SnowDay" of «class cfol» "Desktop" of «class cfol» "Username" of «class cfol» "Users" of «class sdsk»} to alias


That is why I labeled it with Finder Object reference won’t return POSIX path, because I tried what was suggested about using the POSIX path of line. I thought seeing the lines above and around it would help set the context.


So I need those files, but I need them sorted by creation date so that they’re in that order for the next renaming step.

Mar 5, 2015 8:28 PM in response to GeoCo

GeoCo, thank you for jumping in on this with me!


I think I understand what you're saying. Except that the log filteredList from the line above the sort command returns an AppleScript/UNIX like format what looks me exactly AS a list, example:


(*alias Macintosh HD:Users:UserName:Desktop:SnowDay:Aauuttoommaattoorr1.MOV, alias Macintosh HD:Users:UserName:Desktop:SnowDay:Aauuttoommaattoorr2.MOV, alias Macintosh HD:Users:UserName:Desktop:SnowDay:TLC-0001-SnowDays-20081231.AVI*)


and then after that list was sorted, it turned that (what I thought was a) list of aliases into


--> {document file "TC-0001-SnowDays-20081231.AVI" of folder "SnowDay" of folder "Desktop" of folder "UserName" of folder "Users" of startup disk, document file "Aauuttoommaattoorr1.MOV" of folder "SnowDay" of folder "Desktop" of folder "UserName" of folder "Users" of startup disk, document file "Aauuttoommaattoorr2.MOV" of folder "SnowDay" of folder "Desktop" of folder "UserName" of folder "Users" of startup disk}


SoI guess I'm just confused. Now the most basic question I have is: with the understanding (from searching around online) that the structure, "document file... of folder....of folder...etc." is called Finder Object Reference, and is returned as the "specifier : the sorted items in their new order" (according to the Finder Dictionary) , but that the list called for to be sorted was in alias format before that, my question is (drum roll finally): How do I get the list back into alias format from Finder Object Reference format in the right order?

Mar 5, 2015 8:30 PM in response to VikingOSX

I totally understand that you cannot reply every Yahoo who does not understand the instructions in your advice. I would not be able to reply to emailed questions from my patients, but most are over 80 and do not use computers. Thank God for small favors.

I do not know how to ask more questions from ChitlinsCC, who replied to me and pointed out problems with my Biopage. I do not know what Kimono open wide means, and why my problem is quite a different matter.

I like the Viking Ship that you use as your logo. It means that you are prepared to oar a ship accross continents and centuries.

It is nice meeting people like you, however briefly.


Etela Neumenn

Mar 5, 2015 11:19 PM in response to tc8213

You may manually convert Finder objects to aliases like this.



set d to choose folder tell application "Finder" set ff to files of item d as alias list -- ff = alias list set ff1 to sort ff by creation date -- ff1 = list of Finder objects repeat with f in ff1 set f's contents to f as alias end repeat ff1 -- ff1 = alias list end tell



Regards,

H

How to convert from Finder Object reference to POSIX path

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.