Retrieving text from file as string of code

Hi, I do hope someone out there knows how to do this… Im creating an applescript that will get the name of every item in a folder, then save the result in a text file, then retrieve this text back into a variable and then use this text as the original variable before exporting the data, just like nothing happened. unfortunatly I get a lot of errores doing this… here is the script:

tell application "Finder"
	set FileNames to name of every item of folder "MyDocuments" of folder "Desktop" of disk "brian"
	
	set theFilePath to "brian:desktop:thelist.txt"
	-------------------------------------------------------------
	try
		set XXXXX to open for access theFilePath with write permission
		write FileNames to XXXXX
		close access XXXXX
	on error errstr number errNum
		if errNum = -49 then
			close access XXXXX
			set XXXXX to open for access theFilePath with write permission
			write FileNames to XXXXX
			close access XXXXX
		else
			tell current application
				activate
				display dialog errstr
				return false
			end tell
		end if
	end try
	delay 2
	-------------------------------------------------------------
	--lots and lots of stuff
	-------------------------------------------------------------
	try
		set XXXXX to open for access theFilePath with write permission
		set FileNames to (read theFilePath)
		close access XXXXX
	on error errstr number errNum
		if errNum = -49 then
			close access XXXXX
			set XXXXX to open for access theFilePath with write permission
			set FileNames to (read theFilePath)
			close access XXXXX
		else
			tell current application
				activate
				display dialog errstr
				--return false
			end tell
		end if
	end try
	-------------------------------------------------------------
	set X to count of every item in FileNames
	
	repeat with i from 1 to X
		set W to item i of X
		-------------------------------------------------------------
		--lots and lots of stuff
		-------------------------------------------------------------
	end repeat
	
	
end tell

Any idea will be most appreciated :slight_smile:

Brian

You are writing an AppleScript list to file, but you do not read it back as list.
Read the entry for the read command in the Standard Additions dictionary.

And: all of the code is in a Finder tell block, but very little of the code is actually executed by the Finder. All those read/write commands belong to Standard Additions, and should not be in a tell app block.
There’s also a tell current app block which should not be inside a Finder tell.
Another time saver: you do not need to open for access… when reading. Just the read command works fine.

Telling Finder to do things it cannot do will make the script run slower at the least, as AppleScript will have to go looking for another entity to run the code.

Writing files names to a file in the most simplest way is:

do shell script "ls /Volumes/brian/Desktop/MyDocuments > /Volumes/brian/Desktop/mylist.txt"

to open the file you can choose either way a plain applescript solution:

paragraphs of (read file "brian:Desktop:mylist.txt" as «class utf8»)

or use a shell command again (in this case I would suggest the line above).

paragraphs of (do shell script "cat /Volumes/brian/Desktop/mylist.txt")

NOTE: The only forbidden characters in a file name are it’s path separator and the null character (byte 00000000). Linefeeds are allowed in a filenames but rarely used so it “safe” to use the shell in this case. When there is no performance difference I would choose an plain AppleScript solution over a shell command but ls is much much quicker then Finder.

Thank you! very good solutions… but all solutions improve reading the file. I am suming that my write code is just fine! is there a better way to write into a file?

were is the read command? in the dictionary of what application? :frowning:

I am still getting garbage and addign the as «class utf8» when writing send an error that it cant set the data to the specific type :frowning:

Hello

Here is an edited version of the entire script :

set baseFolder to "brian:desktop:" # This way it will be easier to edit if required

tell application "System Events"
	set FileNames to name of every item of folder (baseFolder & "MyDocuments") whose visible is true
end tell
set theFilePath to baseFolder & "thelist.txt"

-------------------------------------------------------------
try
	set XXXXX to open for access theFilePath with write permission
	set eof of XXXXX to 0 # may be good practice to get rid of old contents
	write FileNames to XXXXX
	close access XXXXX
on error errstr number errNum
	if errNum = -49 then
		close access XXXXX
		set XXXXX to open for access theFilePath with write permission
		write FileNames to XXXXX
		close access XXXXX
	else
		tell application "SystemUIServer" # EDITED
			display dialog errstr
			return false
		end tell
	end if
end try
delay 2
-------------------------------------------------------------
--lots and lots of stuff
-------------------------------------------------------------
# # # # # # # # #
try
	set FileNames to (read theFilePath)
on error errstr number errNum
	tell application "SystemUIServer"
		display dialog errstr
		--return false
	end tell
end try
-------------------------------------------------------------
set X to count FileNames
# # # # # # # # #
repeat with i from 1 to X
	set W to item i of X
	-------------------------------------------------------------
	--lots and lots of stuff
	-------------------------------------------------------------
end repeat

Here is an alternate code for the write part :

try
	set XXXXX to open for access theFilePath with write permission
on error errstr number errNum
	if errNum = -49 then
		close access XXXXX
		set XXXXX to open for access theFilePath with write permission
	else
		tell application "SystemUIServer" # EDITED
			display dialog errstr
			return false
		end tell
	end if
end try
# Now the file is correctly open for access, work on it
try
	set eof of XXXXX to 0 # may be good practice to get rid of old contents
	write FileNames to XXXXX
	close access XXXXX
on error errstr number errNum
	close access XXXXX
	tell application "SystemUIServer" # EDITED
		display dialog errstr
		return false
	end tell
end try

But from my point of view, the best one would be what DJ Bazzie Wazzie offered :

set baseFolder to "brian:desktop:" # This way it will be easier to edit if required
set sourceFolder to baseFolder & "MyDocuments"
set theFilePath to baseFolder & "thelist.txt"
do shell script "ls " & quoted form of POSIX path of sourceFolder & " " & quoted form of POSIX path of theFilePath

or

set baseFolder to "brian:desktop:" # This way it will be easier to edit if required
set sourceFolder to baseFolder & "MyDocuments"
set theFilePath to baseFolder & "thelist.txt"
set qpSource to quoted form of POSIX path of sourceFolder
do shell script "ls " & qpSource & " " & quoted form of POSIX path of theFilePath
set FileNames to paragraphs of (do shell script "ls " & qpSource)

I’m quite sure that there is a more efficient way to get FileNames without a second call to do shell script but I’m not at ease with Unix

Yvan KOENIG (VALLAURIS, France) dimanche 20 avril 2014 20:21:52

Thank you very much for your time, effort and knowledge! I was uncapable of making the first example to work! I get a error when trying to read the file! but the other 2 examples read the file perfectly! I will use the last option its the best :smiley:

Oops, I forgot something.

At the very beginning,

set theFilePath to baseFolder & “thelist.txt”

must be :

set theFilePath to file (baseFolder & “thelist.txt”)

Yvan KOENIG (VALLAURIS, France) lundi 21 avril 2014 10:31:31

Im still getting errors pleas help!!! :expressionless:

The script works just grate but when working with the name of the files! if you change name for desktop position it only saves garbage.

This is the script:

set baseFolder to "Users:briand:desktop:" # This way it will be easier to edit if required

tell application "Finder"
	set FileNames to desktop position of every item of folder baseFolder & "mypictures"
end tell
set theFilePath to baseFolder & "thelist.txt"

-------------------------------------------------------------
try
	set XXXXX to open for access theFilePath with write permission
	set eof of XXXXX to 0 # may be good practice to get rid of old contents
	write FileNames to XXXXX
	close access XXXXX
on error errstr number errNum
	if errNum = -49 then
		close access XXXXX
		set XXXXX to open for access theFilePath with write permission
		write FileNames to XXXXX
		close access XXXXX
	else
		tell application "SystemUIServer" # EDITED
			display dialog errstr
			return false
		end tell
	end if
end try

This is the text as it appears in the script

	get desktop position of every item of folder "brian:Desktop:mypictures"
		--> {{183, 1181}, {97, 1196}, {340, 1195}, {259, 1184}, {2288, 1004}, {476, 1302}, {407, 1302}, {551, 1303}, {338, 1302}, {445, 339}, {84, 350}, {85, 224}, {2302, 330}, {2097, 341}, {556, 83}, {79, 474}, {85, 106}, {556, 210}, {450, 587}, {1723, 949}, {1980, 1083}, {2308, 96}, {2477, 831}, {2306, 622}, {2295, 867}, {1272, 923}, {2471, 583}, {1723, 815}, {1717, 336}, {1980, 952}, {243, 96}, {2302, 210}, {445, 457}, {447, 97}, {447, 215}, {1724, 689}, {1724, 453}, {1720, 205}, {1720, 87}, {243, 232}, {1901, 340}, {228, 770}, {243, 350}, {2096, 218}, {1902, 83}, {238, 630}, {2255, 1221}, {240, 491}, {1900, 219}, {2297, 745}, {1900, 459}, {2094, 76}, {99, 1305}, {2479, 704}, {2476, 965}, {262, 1303}, {187, 1286}, {2302, 464}, {1977, 829}, {1721, 574}, {1979, 1207}}

and this is the text saved into the file:

someone pleas help!!! :expressionless:

ho!!! I had to change in the fist part of the script: tell application “System Events” to tell application “finder” to use the desktop position command.

Brian

Are you serious or are you joking ?

Your original message was speaking of names.
Now you speak of position of icons in a window.

(1) For sure, System Events can’t do that, it doesn’t apply upon the Finder’s windows.
(2) set FileNames to desktop position of every item of folder (basefolder & “mypictures”) return a list of missing value.
(3) set FileNames to position of every item of folder (basefolder & “mypictures”) return a list of positions which, as you are supposed to know are lists of two numbers.
Writing such list give the useless result which you described.

You must convert the datas in text.
It’s what is done in this script.

set basefolder to path to desktop folder as text

tell application "Finder"
	open folder (basefolder & "mypictures")
	set FileNames to position of every item of window of folder (basefolder & "mypictures")
end tell

set theFilePath to basefolder & "thelist.txt"

repeat with aPosition in FileNames
	# convert every position into a string
	set contents of aPosition to my recolle(aPosition, ", ")
end repeat
# Convert the list of strings into a string
set FileNames to my recolle(FileNames, return)

-------------------------------------------------------------
try
	set XXXXX to open for access theFilePath with write permission
	set eof of XXXXX to 0 # may be good practice to get rid of old contents
	write FileNames to XXXXX
	close access XXXXX
on error errstr number errNum
	if errNum = -49 then
		close access XXXXX
		set XXXXX to open for access theFilePath with write permission
		write FileNames to XXXXX as list
		close access XXXXX
	else
		tell application "SystemUIServer" # EDITED
			display dialog errstr
			return false
		end tell
	end if
end try

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to "" & l
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

The Event log is :

tell current application
	path to desktop as text
		--> "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:"
end tell
tell application "Finder"
	open folder "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:mypictures"
	get position of every item of window of folder "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:mypictures"
		--> {{154, 29}, {53, 29}, {164, 165}, {255, 29}, {375, 141}, {457, 29}, {558, 29}}
end tell
tell current application
	open for access "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:thelist.txt" with write permission
		--> 1088
	set eof 1088 to 0
	write "154, 29
53, 29
164, 165
255, 29
375, 141
457, 29
558, 29" to 1088
	close access 1088
end tell

Yvan KOENIG (VALLAURIS, France) mardi 22 avril 2014 00:18:01

sorry for the uselessness :expressionless: but thank you for your help! :slight_smile:

I retrieved an info which I searched two days ago.


set basefolder to path to desktop folder as text

tell application "Finder"
	open folder (basefolder & "mypictures")
	set FilePositions to position of every item of window of folder (basefolder & "mypictures")
end tell

set theFilePath to basefolder & "thelist.txt"
# Write the list in a text file. CAUTION : the dataType is defined as list
my writeto(theFilePath, FilePositions, list, false)
# Read the contents of the text file without defining the correct data type
set File_Positions to read file theFilePath

# Read the list. CAUTION : the data type is set to list
set File_Positions to read file theFilePath as list

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

The Event log is :

tell current application
	path to desktop as text
		--> "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:"
end tell
tell application "Finder"
	open folder "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:mypictures"
	get position of every item of window of folder "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:mypictures"
		--> {{154, 29}, {53, 29}, {164, 165}, {255, 29}, {375, 141}, {457, 29}, {558, 29}}
end tell
tell current application
	open for access file "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:thelist.txt" with write permission
		--> 102
	set eof 102 to 0
	write {{154, 29}, {53, 29}, {164, 165}, {255, 29}, {375, 141}, {457, 29}, {558, 29}} to 102 starting at eof as list
	close access 102
	read file "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:thelist.txt"
		--> "listlistlong longlistlong5longlistlong long listlong longlistlongwlongçlistlong.longlistlong.long"
		--> I edited the string a bit because some characters fooled the message editor
	read file "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Desktop:thelist.txt" as list
		--> {{154, 29}, {53, 29}, {164, 165}, {255, 29}, {375, 141}, {457, 29}, {558, 29}}
end tell

As you may see, defining the correct datatype is important.

Yvan KOENIG (VALLAURIS, France) mardi 22 avril 2014 21:21:21

Thank you very much!!! :smiley: