Variable Modification Problems

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

Won’t Let me post contents of my metadata file but basically just:

Lifestyle “bar” Ryan Smith “Bar” Yes

And where is the delimiter pipe “|” ?

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

Yes I am able to display the value of p but I cannot rename the desktop file metadata.txt or any folder for that matter with the value of “p”

Yes pipes are used in the metadata.txt file

Is your text file MacRoman encoded or perhaps UTF-16?

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

Thanks for your help

This creates a MacRoman encoded file.
I tried the script in your first post and it works fine on my machine even the renaming

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?

without any information about the Studio connection, no

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

Again thanks for your help

Thanks, but it doesn’t help at all.

In your first script the metadata file is located on desktop,
in the second at “Tiki Drop:External Dropbox:.”

Do you get any error message?

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?

:lol:

we could have saved an hour, if you had said this right at the beginning.

The error message means:
You’re trying to rename a file by a name, which already exist in this directory,

That’s all

Oh I wish that were the case, however there is no file with that name on the Desktop or the directory :confused:

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 terminal.app

Type file -b and a space character, then drag the file into the terminal window and press return
This displays the encoding of the file

Simply says “data”

alternative method:

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

That came up with Western (Mac OS Roman) so I am assuming that should be good right?

Again, thanks for all your time