Basically I am trying to read the file, create variables from the list, then rename files based upon these variables, however it will not let me. I have tried everything I can think of and looked all over the web.
set theFile to (path to desktop as Unicode text) & "metadata.txt"
set thePath to (path to desktop as Unicode text)
set parsed_text to read file theFile as text from 1 to eof using delimiter "|"
set p to item 1 of parsed_text as text --doesn't work
set b to "test" as text --works
tell application "Finder"
try
set the name of file theFile to (b & ".txt")
on error the error_message number the error_number
set the error_message to error_message
end try
end tell
this works for me, if the textfile contains “alpha|beta|gamma” with text encoding MacRoman
set theFile to (path to desktop as Unicode text) & "metadata.txt"
set parsed_text to read file theFile using delimiter "|"
set p to item 2 of parsed_text
--> beta
Note: either as text or from 1 to eof is not needed.
You can read the text as string, «class utf8» or Unicode text
I am not sure the file is created by a different script using this code:
set collectionfolder to "Tiki Drop:External Dropbox:"
set collectionfolder2 to "Tiki Drop:External Dropbox:" & destination_folder
if folder_found is equal to "No" then
tell application "Finder"
make new folder at collectionfolder with properties {name:destination_folder}
end tell
end if
set theFile to "metadata.txt"
set thePath to collectionfolder2 & ":" & theFile
set fRef to (open for access file thePath with write permission)
try
set eof fRef to 0
if type is "Lifestyle" then
write "Lifestyle|" & photographer_name & "|" & photo_contact & "|" & shoot_name & "|" & shoot_date & "|" & expiration_date & "|" & photo_producer & "|" & gd_project_number & "|" & keywords & "|" & usage_rights & "|" & with_product & "|" & with_talent & "" to fRef
else if type is "Product" then
write "Product|" & photographer_name & "|" & photo_contact & "|" & shoot_name & "|" & shoot_date & "|" & expiration_date & "|" & photo_producer & "|" & gd_project_number & "|" & keywords & "|" & usage_rights & "" to fRef
else if type is "Stock" then
write "Stock|" & image_house & "|" & original_file & "|" & expiration_date & "|" & photo_producer & "|" & gd_project_number & "|" & keywords & "|" & usage_rights & "" to fRef
end if
end try
close access fRef
Yes, when I manually type in some pipe delimted data I was able to get it working just as you. However when use the file that is generated by the script, no good. All these variables are being populated by UI elements in a Applescript Studio interface. Any Ideas?
Here is where each of the variables are being declared prior to writing the file: I hope this helps:
set talent_switch to contents of button "talent"
set type to contents of text field "type"
set photographer_name to contents of text field "photographer"
set shoot_name to contents of text field "shoot_name"
set con_producto to contents of button "contains_product"
if con_producto is true then
set with_product to "Yes"
else
set with_product to "No"
end if
if talent_switch is true then
set with_talent to "Yes"
else
set with_talent to "No"
end if
set shoot_month to contents of combo box "shoot_month"
set shoot_year to contents of combo box "shoot_year"
if shoot_month is "" then
else
set shoot_date to shoot_month & "-" & shoot_year
end if
set expiration_month to contents of combo box "expiration_month"
set expiration_year to contents of combo box "expiration_year"
if expiration_month is "" then
set expiration_date to "None"
else
set expiration_date to expiration_month & "-" & expiration_year
end if
set gd_project_number to contents of text field "gd_project_number"
set photo_producer to contents of text field "photo_producer"
set original_file to contents of text field "original_file"
set image_house to contents of text field "image_house"
end try
end tell
tell text view "photographer_contact" of scroll view "photographer_contact" of window of theObject
set photo_contact to content
end tell
tell text view "keywords" of scroll view "keywords" of window of theObject
set keywords to content
end tell
tell table view "usage_rights" of scroll view "usage_rights" of window of theObject
set selectedDataRows to get selected data rows of table view "usage_rights" of scroll view "usage_rights" of window of theObject
set firstDataRow to item 1 of selectedDataRows
set usage_rights to get content of data cell 1 of firstDataRow
end tell
I was trying to simply the process by just putting the exact metadata file I am working with on the desktop and running a smaller version of the script. I am getting “The operation could not be completed because there is already an item with that name.” currently. Is there any way to format the text to Mac Roman upon creation?
Oh I wish that were the case, however there is no file with that name on the Desktop or the directory
If I switch gear and use this code
set theFile to (path to desktop as Unicode text) & "metadata.txt"
set thePath to (path to desktop as Unicode text) & "Test Folder"
set parsed_text to read file theFile as text from 1 to eof using delimiter "|"
set p to item 1 of parsed_text as text --doesn't work
set b to "test" as text --works
tell application "Finder"
try
set the name of folder thePath to p
on error the error_message number the error_number
set the error_message to error_message
end try
end tell
I get this error: "Finder got an error: Can’t set name of file "Macintosh HD:Users:ryan:Desktop:Test Folder" to "
Exact retype of metadata file “Lifestyle|Ryan Nosek|402 Market St.| M353|04-2019|10-2036|Jim Smith|323232A|Taco, Burrito, Delicious|1 year worldwide unlimited usage, excluding advertising.|Yes”
It is working if I type it in manually, but if used generated document, no good, How can I check/change encoding
open the file in TextEdit.app and choose Save as. from File menu.
The displayed encoding at the Plain Text Encoding pop up is the native encoding of the file