Log/Text File Permissions...

I am trying to incorporate a “logging capability” into my scripts both as a learning exercise and to help with debugging [i.e. specifically, I am testing the reliability of pinging my iPhone].

I have created the following script:


-- Get current date in YYYYMMDD format	
set currentDate to current date
set currentDateYear to year of currentDate
set currentDateMonth to month of currentDate as integer

if ((day of currentDate < 10) is true) then
	set currentDateDay to "0" & day of currentDate
else
	set currentDateDay to day of currentDate
end if

set currentDateYYYYMMDD to currentDateYear & currentDateMonth & currentDateDay as string


tell application "TextEdit"	
	activate
	
	make new document
	set theDocument to document 1
	
	set testText1 to "this is the first line of text"
	set testText2 to "this is the second line of text"
	
        set testTab1 to "this is tab 1"
	set testTab2 to "this is tab 2"
	
	set paragraph 1 of theDocument to testText1 & "\t" & "\t" & testTab1 & "\r"
	set paragraph 2 of theDocument to testText2 & "\t" & testTab2 & "\r"
		
	-- Set the name of the text file to be saved
	set userName to do shell script "whoami"
	set fileName to "Macintosh HD:Users:" & userName & ":Desktop:" & currentDateYYYYMMDD & "_SONOS auto pause play log.txt"
	
	-- Save the document
	save theDocument in fileName
	
end tell

Although I can code most of the framework I want I am running into two problems:

  1. I cannot save the file…to be specific, when I attempt to save the file I get the error message The document “Untitled” could not be exported as “20141223_SONOS auto pause play log.txt”. You don’t have permission. To view or change permissions, select the item in the Finder and choose File > Get Info.…while I did look in the TextEdit Dictionary for permissions I could not find any relevant parameters, etc. [Edit: I figured this one out…I need to change " theDocument in fileName" to "theDocument in file fileName].

  2. I don’t know whether it is better to create a log file as a text file [as I am trying to do and as I se most often done] or is it better to create a log file as an Excel file as the date can be more easily manipulated.

Thanks in advance,

Joel

Hi,

this is easier without TextEdit.
The whoami stuff is not necessary, there are a lot of relative path specifiers (beginning with path to)


set desktopFolder to path to desktop as text -- HFS path to desktop of the current user
set currentDateYYYYMMDD to do shell script "date +%Y%m%d"

set paragraphList to {}
set testText1 to "this is the first line of text"
set testText2 to "this is the second line of text"

set testTab1 to "this is tab 1"
set testTab2 to "this is tab 2"

set end of paragraphList to testText1 & "	" & "	" & testTab1
set end of paragraphList to testText2 & "	" & testTab2

set fileName to desktopFolder & currentDateYYYYMMDD & "_SONOS auto pause play log.txt"
set {TID, text item delimiters} to {text item delimiters, return}
set theText to paragraphList as text
set text item delimiters to TID
try
	set fileRef to open for access file fileName with write permission
	write theText to fileRef
	close access fileRef
on error
	try
		close access file fileName
	end try
end try

Wow, I must really be doing something wrong…

In the below AppleScript I tried to embed the code I wrote for logging but it is not working in that TextEdit not evening opening which I do not understand as it worked as a standalone AS but is not working when embedded in a larger AS…how do I fix this?


-- Set global variables.  Definitions are defined below
global currentDateYYYYMMDD, firstDetection, IP_address, paragraphCount

on run
	set firstDetection to true -- Set flag to true so that the first time the iPhone is detected the SONOS Music System is started	
	set IP_address to "192.168.2.11" -- Set the static IP address of the iPhone on the LAN
	set paragraphCount to 3 -- Set the first line of the log file on which text appears
	
	-- Get current date in YYYYMMDD format which is used in naming the log file
	set currentDate to current date
	set currentDateYear to year of currentDate
	set currentDateMonth to month of currentDate as integer
	
	if ((day of currentDate < 10) is true) then
		set currentDateDay to "0" & day of currentDate
	else
		set currentDateDay to day of currentDate
	end if
	
	set currentDateYYYYMMDD to currentDateYear & currentDateMonth & currentDateDay as string
	
end run


on idle
	set currentTimeEntry1 to time of (current date)
	
	-- Ping for absence/presence of iPhone [i.e. me] which triggers start/stop of SONOS Music system
	-- Ping twice to account for / work around the iPhone occasional WiFi connection loss
	set ping1 to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
	if (ping1 contains "ms") then -- Included for loggig purposes
		set ping1Entry to true
	else
		set ping1Entry to false
	end if
	delay 120
	set ping2 to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
	if (ping2 contains "ms") then -- Included for loggig purposes
		set ping2Entry to true
	else
		set ping2Entry to false
	end if
	
	-- Set results of ping for absence/presence of iPhone to iPhoneDetected
	if ((ping1 contains "ms") or (ping2 contains "ms")) then
		set iPhoneDetected to true
		-- display dialog ("iPhone Detected - " & ping) buttons {"Okay"} default button 1 with title "iPhone Detection Test" -- Commented out, included for testing purposes only
		-- display dialog ("first Detection Before Action - " & firstDetection) buttons {"Okay"} default button 1 with title "iPhone firstDetection Test" -- Commented out, included for testing purposes only
		-- set firstDetectionEntry1 to firstDetection	-- Included for loggig purposes		
	else if ((ping1 contains "timeout") and (ping2 contains "timeout")) then
		set iPhoneDetected to false
		-- display dialog "iPhone Not Detected " buttons {"Okay"} default button 1 with title "iPhone Detection Test" -- Commented out, included for testing purposes only
		-- display dialog ("first Detection Before Action - " & firstDetection) buttons {"Okay"} default button 1 with title "iPhone firstDetection Test" -- Commented out, included for testing purposes only	
		-- set firstDetectionEntry1 to firstDetection	-- Included for loggig purposes			
	end if
	
	-- When result of ping for absence/presence of iPhone is absent then run the script to pause/stop SONOS Music system
	if ((iPhoneDetected is equal to false) and (firstDetection is equal to false)) then
		set firstDetection to true -- Set flag to true so that the next time the iPhone is present the SONOS Music System is started
		set callSONOSPause to "/Users/JoelC/Documents/Apple/Scripts/SONOS Pause Play/20141220_script to pause sonos_complete.scpt" as string
		run script callSONOSPause
		-- display dialog ("first Detection After callSONOSPause- " & firstDetection) buttons {"Okay"} default button 1 with title "iPhone firstDetection Test" -- Commented out, included for testing purposes only
		set firstDetectionEntry2 to firstDetection -- Included for logging purposes
		set scriptEntry to "SONOSPause" -- Included for logging purposes
	end if
	
	-- When result of ping for absence/presence of iPhone is presnet then run the script to play / start SONOS Music system
	if ((iPhoneDetected is equal to true) and (firstDetection is equal to true)) then
		set firstDetection to false -- Set flag to false so that the next time the iPhone is present without first being absent nothing happens
		set callSONOSVolume to "/Users/JoelC/Documents/Apple/Scripts/SONOS Pause Play/20141220_script for a preset volume_complete.scpt" as string
		run script callSONOSVolume
		set callSONOSPlay to "/Users/JoelC/Documents/Apple/Scripts/SONOS Pause Play/20141220_script to play sonos_complete.scpt" as string
		run script callSONOSPlay
		-- display dialog ("first Detection After callSONOSPlay - " & firstDetection) buttons {"Okay"} default button 1 with title "iPhone firstDetection Test" -- Commented out, included for testing purposes only
		set firstDetectionEntry2 to firstDetection -- Included for logging purposes
		set scriptEntry to "SONOSPlay" -- Included for logging
	end if
	
	
	-- Set the idle time between pings to test for the absence/presence of the iPhone
	set timeControl to time of (current date)
	
	if ((timeControl is greater than 72000) or (timeControl is less than 28800)) then -- No ping to test for iPhone absence/presence between 8:00 PM and 8:00 AM	
		if (timeControl is greater than 72000) then set returnTime to ((86339 - timeControl) + 28800) as integer
		if (timeControl is less than 28800) then set returnTime to (28800 - timeControl) as integer
		return returnTime
	else
		if iPhoneDetected is equal to false then set returnTime to 60 as integer -- Ping for absence/presence of iPhone every 1 minute when iPhone is absent
		if iPhoneDetected is equal to true then set returnTime to 300 as integer -- Ping for absence/presence if iPhone every 5 minutes when iPhone is present
		-- display dialog ("returnTime - " & returnTime) buttons {"Okay"} default button 1 with title "Retun Time Value" -- Commented out, included for testing purposes only
		return returnTime
	end if
	
	set currentTimeEntry2 to time of (current date)
	set returnTimeEntry to returnTime
	
	-- Create log file to test consistency/reliability of ping for absence/presence of the iPhone 	
	tell application "TextEdit"
		
		if (paragraphCount is equal to 3) then
			activate
			set theDocument to document 1
		end if
		
		set paragraph paragraphCount of theDocument to currentTimeEntry1 & " " & ping1Entry & " " & ping2Entry & " " & iPhoneDetected & " " & firstDetectionEntry1 & " " & scriptEntry & " " & firstDetectionEntry2 & " " & returnTimeEntry & " " & currentTimeEntry2
		
		set paragraphCount to paragraphCount + 1
		
		-- Set the name of the text file to be saved
		if (paragraphCount is equal to 4) then
			set userName to do shell script "whoami"
			set fileName to "Macintosh HD:Users:" & userName & ":Desktop:" & currentDateYYYYMMDD & "_SONOS auto pause play log.txt"
		end if
		
		-- Save the document
		save theDocument in file fileName
		
	end tell
	
end idle


on quit
	display dialog "Do you also want to pause/stop the SONOS Music System?" with title "SONOS Music System Automatic Pause / Play" buttons {"Yes", "No"} default button 2 with icon 1 giving up after 15
	set pauseMusic to button returned of result
	if (pauseMusic is equal to "Yes") then
		set callSONOSPause to "/Users/JoelC/Documents/Apple/Scripts/SONOS Pause Play/20141220_script to pause sonos_complete.scpt" as string
		run script callSONOSPause
	end if
	
	display dialog "Exit/stopping the SONOS Music System pause/play script" with title "SONOS Music System Automatic Pause / Play" buttons {"Okay"} default button 1 with icon 1 giving up after 5
	continue quit
end quit


Thanks in advance,

Joel

PS. Stefan, I am looking at your code to see whether I can modify this for my purposes…appreciate the suggestion!

PS: you can easily append a line to the present log file by changing this line


write theText to fileRef starting at eof

Appreciate that an still looking at your code…any idea why the section of code I added which starts with “tell application TextEdit” and end with the related “end tell” is not working [i.e. TextEdit is not even opening]?

Thx…

wrap the code in a try block and catch the error


on idle()
	try
		
		-- code
		
	on error e
		display dialog e
	end try
end idle

Stefan:

I have – in the below script – taken your approach as I love its elegance and ease but alas no log file is produced…I did split the approach into two in that i) the “collection of the information” is in the 'On Idle" section and ii) the “file creation” is in the “On Quit” portion.

I would greatly appreciate the benefit of you expertise as to how to fix this.

Thanks.


-- Set global variables.  Definitions are defined below
global currentDateYYYYMMDD, desktopFolder, firstDetection, IP_address, paragraphList

on run
	set currentDateYYYYMMDD to do shell script "date +%Y%m%d"
	set desktopFolder to path to desktop as text -- Set path to desktop folder as HFS
	set firstDetection to true -- Set flag to true so that the first time the iPhone is detected the SONOS Music System is started	
	set IP_address to "192.168.2.11" -- Set the static IP address of the iPhone on the LAN
	set paragraphList to {} -- 
	
end run


on idle
	set currentTimeEntry1 to time of (current date)
	
	-- Ping for absence/presence of iPhone [i.e. me] which triggers start/stop of SONOS Music system
	-- Ping twice to account for / work around the iPhone occasional WiFi connection loss
	set ping1 to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
	if (ping1 contains "ms") then -- Included for loggig purposes
		set ping1Entry to true
	else
		set ping1Entry to false
	end if
	delay 120
	set ping2 to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
	if (ping2 contains "ms") then -- Included for loggig purposes
		set ping2Entry to true
	else
		set ping2Entry to false
	end if
	
	-- Set results of ping for absence/presence of iPhone to iPhoneDetected
	if ((ping1 contains "ms") or (ping2 contains "ms")) then
		set iPhoneDetected to true
		-- display dialog ("iPhone Detected - " & ping) buttons {"Okay"} default button 1 with title "iPhone Detection Test" -- Commented out, included for testing purposes only
		-- display dialog ("first Detection Before Action - " & firstDetection) buttons {"Okay"} default button 1 with title "iPhone firstDetection Test" -- Commented out, included for testing purposes only
		-- set firstDetectionEntry1 to firstDetection	-- Included for loggig purposes		
	else if ((ping1 contains "timeout") and (ping2 contains "timeout")) then
		set iPhoneDetected to false
		-- display dialog "iPhone Not Detected " buttons {"Okay"} default button 1 with title "iPhone Detection Test" -- Commented out, included for testing purposes only
		-- display dialog ("first Detection Before Action - " & firstDetection) buttons {"Okay"} default button 1 with title "iPhone firstDetection Test" -- Commented out, included for testing purposes only	
		-- set firstDetectionEntry1 to firstDetection	-- Included for loggig purposes			
	end if
	
	-- When result of ping for absence/presence of iPhone is absent then run the script to pause/stop SONOS Music system
	if ((iPhoneDetected is equal to false) and (firstDetection is equal to false)) then
		set firstDetection to true -- Set flag to true so that the next time the iPhone is present the SONOS Music System is started
		set callSONOSPause to "/Users/JoelC/Documents/Apple/Scripts/SONOS Pause Play/20141220_script to pause sonos_complete.scpt" as string
		run script callSONOSPause
		-- display dialog ("first Detection After callSONOSPause- " & firstDetection) buttons {"Okay"} default button 1 with title "iPhone firstDetection Test" -- Commented out, included for testing purposes only
		set firstDetectionEntry2 to firstDetection -- Included for logging purposes
		set scriptEntry to "SONOSPause" -- Included for logging purposes
	end if
	
	-- When result of ping for absence/presence of iPhone is presnet then run the script to play / start SONOS Music system
	if ((iPhoneDetected is equal to true) and (firstDetection is equal to true)) then
		set firstDetection to false -- Set flag to false so that the next time the iPhone is present without first being absent nothing happens
		set callSONOSVolume to "/Users/JoelC/Documents/Apple/Scripts/SONOS Pause Play/20141220_script for a preset volume_complete.scpt" as string
		run script callSONOSVolume
		set callSONOSPlay to "/Users/JoelC/Documents/Apple/Scripts/SONOS Pause Play/20141220_script to play sonos_complete.scpt" as string
		run script callSONOSPlay
		-- display dialog ("first Detection After callSONOSPlay - " & firstDetection) buttons {"Okay"} default button 1 with title "iPhone firstDetection Test" -- Commented out, included for testing purposes only
		set firstDetectionEntry2 to firstDetection -- Included for logging purposes
		set scriptEntry to "SONOSPlay" -- Included for logging
	end if
	
	
	-- Set the idle time between pings to test for the absence/presence of the iPhone
	set timeControl to time of (current date)
	
	if ((timeControl is greater than 72000) or (timeControl is less than 28800)) then -- No ping to test for iPhone absence/presence between 8:00 PM and 8:00 AM	
		if (timeControl is greater than 72000) then set returnTime to ((86339 - timeControl) + 28800) as integer
		if (timeControl is less than 28800) then set returnTime to (28800 - timeControl) as integer
		return returnTime
	else
		if iPhoneDetected is equal to false then set returnTime to 60 as integer -- Ping for absence/presence of iPhone every 1 minute when iPhone is absent
		if iPhoneDetected is equal to true then set returnTime to 300 as integer -- Ping for absence/presence if iPhone every 5 minutes when iPhone is present
		-- display dialog ("returnTime - " & returnTime) buttons {"Okay"} default button 1 with title "Retun Time Value" -- Commented out, included for testing purposes only
		return returnTime
	end if
	
	set currentTimeEntry2 to time of (current date)
	set returnTimeEntry to returnTime
	
	-- Create log file to test consistency/reliability of ping for absence/presence of the iPhone 	
	
	set end of paragraphList to currentTimeEntry1 & " " & ping1Entry & " " & ping2Entry & " " & iPhoneDetected & " " & firstDetectionEntry1 & " " & scriptEntry & " " & firstDetectionEntry2 & " " & returnTimeEntry & " " & currentTimeEntry2
	
end idle


on quit
	
	set fileName to desktopFolder & currentDateYYYYMMDD & "_SONOS auto pause play log.txt"
	set {TID, text item delimiters} to {text item delimiters, return}
	set theText to paragraphList as text
	set text item delimiters to TID
	
	try
		set fileRef to open for access file fielName with write permission
		write theText to fileRef
		close access fileRef
	on error
		try
			close access file fileName
		end try
	end try
	
	
	display dialog "Do you also want to pause/stop the SONOS Music System?" with title "SONOS Music System Automatic Pause / Play" buttons {"Yes", "No"} default button 2 with icon 1 giving up after 15
	set pauseMusic to button returned of result
	if (pauseMusic is equal to "Yes") then
		set callSONOSPause to "/Users/JoelC/Documents/Apple/Scripts/SONOS Pause Play/20141220_script to pause sonos_complete.scpt" as string
		run script callSONOSPause
	end if
	
	display dialog "Exit/stopping the SONOS Music System pause/play script" with title "SONOS Music System Automatic Pause / Play" buttons {"Okay"} default button 1 with icon 1 giving up after 5
	continue quit
end quit

Thanks so much…

One error could be that time of a AppleScript date returns an integer value but you want to write text.
The first item of a text concatenation must be text, later integers are implicitly coerced to text


set end of paragraphList to (currentTimeEntry1 as text) & .

PS: to debug code in a on idle handler run the code directly as a run handler or insert log, say oder display dialog statements

Appreciate the suggestion but that did not work and because it is written in “On Idle” format it is very tough to debug…I am started to get frustrated at this point but will keep looking…

Stefan:

I have made progress in that I now know that the On Idle portion works as I ran it separately changing returnTime to delay so within a repeat loop…good to know.

The problem seems to be in the On Quit portion noting that when I run this separately [i.e. after completing a repeat loop which contains the main code] I get a message that the document cannot be closed while the script is running. ASSUMING this is also the problem with the On Quit section of the script can you please take a look at this and let me know what you think.

Thanks so much,

Joel

there is a typo (fielName)

change

set fileRef to open for access file fielName with write permission

to

set fileRef to open for access file fileName with write permission

Yup, there is…great minds think alike, I just found this myself!!!

Thank you so very much…

Joel

PS. I guess in addition to learning the code / syntax I need to learn how best to interpret the various error messages in that the one listed should have pointed me to the filename!

PPS. Once agin thanks for the very elegant solution, much simpler than mine…

Stefan:

Apologies but I am back – and embarrassingly so…

When I run the script without the On Idle everything is good including getting a text file that is populated…when I run the script with the On Idle the resulting text file is not populated…I took a closer look at the script ad not that i) I find no typos wrt paragraphList ii) I find no further typos in the On Quit portion so am at a lost.

I would be grateful if you could take a fast look.

Thanks!

[b][i][EDIT: Problem solved…the paragraphList was never been created because the code “return returnTime” preceded list entries…I have since moved the code “return returnTime” to the end of the “On Idle” block and all is well…and, of course, this is also the reason my original approach did not work [i.e. I was never getting to the “the Tell block”.

And, while I am at it, apologies for being obtuse, I should have figured this our earlier and on my own but at least I got it!

And for those who are interested in the script’s log file the output can be found at https://www.dropbox.com/s/xif4m58gy9q197d/Screen%20Shot%202014-12-23%20at%2010.46.08%20PM.png?dl=0 noting (in addition to the fact that formatting is not fun) that I will use the log file’s output to tweak some of the coding / “time settings” to increase the script’s consistency and reliability [i.e. iPhone is 95% reliable as a trigger to pause / play the SONOS Music System but there is the odd WiFi dropout which I is the reason I added both the delay between the second ping test as well as the second ping!].

And finally, appreciation and thanks to all who have helped – a big thanks to StefanK – and read this thread!]
[/i][/b]

this is an improved code for logging the information

change the beginning of the script to


-- Set global variables. Definitions are defined below
property header : "STime" & tab & "FPing" & tab & "SPing" & tab & "iPhone" & tab & "FDetect" & tab & "Script" & tab & tab & "SDetect" & tab & tab & "RTime" & tab & "ETime" & return

global firstDetection, IP_address

on run
   set firstDetection to true -- Set flag to true so that the first time the iPhone is detected the SONOS Music System is started    
   set IP_address to "192.168.2.11" -- Set the static IP address of the iPhone on the LAN
end run

replace


-- Create log file to test consistency/reliability of ping for absence/presence of the iPhone    
   
   set end of paragraphList to currentTimeEntry1 & " " & ping1Entry & " " & ping2Entry & " " & iPhoneDetected & " " & firstDetectionEntry1 & " " & scriptEntry & " " & firstDetectionEntry2 & " " & returnTimeEntry & " " & currentTimeEntry2

with


-- Create log file to test consistency/reliability of ping for absence/presence of the iPhone

sonosLog({currentTimeEntry1, ping1Entry, ping2Entry, iPhoneDetected, firstDetectionEntry1, scriptEntry, firstDetectionEntry2, returnTimeEntry, currentTimeEntry2})

remove this portion in the on quit handler


   set fileName to desktopFolder & currentDateYYYYMMDD & "_SONOS auto pause play log.txt"
   set {TID, text item delimiters} to {text item delimiters, return}
   set theText to paragraphList as text
   set text item delimiters to TID
   
   try
       set fileRef to open for access file fielName with write permission
       write theText to fileRef
       close access fileRef
   on error
       try
           close access file fileName
       end try
   end try

add a new handler after end quit
The handler adds the header line automatically when a new file is created
and adjusts the number of tabs between SDetect and RTime

on sonosLog(parameterList)
	if (length of (item 7 of parameterList)) < 7 then
		set parameterList to items 1 thru 7 of parameterList & "" & items 8 thru -1 of parameterList
	end if
	set {TID, text item delimiters} to {text item delimiters, tab}
	set theText to parameterList as text
	set text item delimiters to TID
	set desktopFolder to path to desktop as text
	set currentDateYYYYMMDD to do shell script "date +%Y%m%d"
	set fileName to desktopFolder & currentDateYYYYMMDD & "_SONOS auto pause play log.txt"
	tell application "System Events" to set createNewFile to not (exists file fileName)
	
	try
		set fileRef to open for access file fileName with write permission
		if createNewFile then write header to fileRef
		write (theText & return) to fileRef starting at eof
		close access fileRef
	on error
		try
			close access file fileName
		end try
	end try
end sonosLog

Stefan:

Appreciate the suggested code which I will incorporate into my script though I do need to look at and understand the automatic formatting aspect…

Joel

it’s quite easy.
The log handler checks if a file with the current date exists
If yes, the parameter line will be just appended to the file contents.
If no, a new file will be created and the header line is written out.

In the field “SDetect” an extra tab must be inserted if the length of value is less than 7 characters.
Actually an empty string is inserted but then the list is coerced to text via text item delimiters with delimiter tab

PS: please don’t quote always the entire post content

Stefan K:

Appreciate all the assistance and have everything working [could no have done it without you]…love the code to append to the script should it already exists.

Two new points:

  1. As far as the icon is concerned I am trying to change the icons that i) appear in the Dock and ii) the icons that appear in the dialog box.

I found the following icons https://www.dropbox.com/s/jm2mfcp1lj3adm6/20141224092945177_easyicon_net_48.icns?dl=0.

I tried changing them by i) replacing the package’s applet.ins file with the above file and renaming it to applet.ins [which does change the icons in the dialog boxes] and ii) copying and pasting the above icons over the app’s icons in FInder’ Get Info dialog box…none of these change the icons on the Dock…woud appreciate some guidance as to how best to do this?

  1. I am going to add an “On Reopen” section to get a dialog box to pop open for simple volume changes…I want to try this on my own so please – at least for now – don’t provide me with the code bit, if there is a better route to take, please let me know that.

Thanks again,

Joel

I continue to refine the coding of my script to clean it up, to make it more efficient and to improve the log information…in so doing there is a small section of code that I am not entirely clear about, namely:


  set {TID, text item delimiters} to {text item delimiters, return}
  set theText to parameterList as text
  set text item delimiters to TID

  write theText to fileRef

I understand text item delimiters [i.e. they provide a convenient way of separating data that is delimited or separated by a specified character into components], I understand that the text items delimiters is being changed from its default value [i.e. null string] to a return character.

What I am not sure I understand completely – and would very much appreciated an explanation of – is why the change in the text item delimiters results in theText being written to the file with each entry appearing on a separate line…with that please confirm or correct the following understanding:

  1. The code " set theText to parameterList as text" results in each item in parameterList being written to the variable theText but because the text item delimiters is a return character each item is separated by a return character (rather than the null character).

  2. The code “write theText to fileRef” results in the theText being written to the text file “fileRef” [which was previously defined] but because the “text items” are separated by a return character the text editor application interprets the return character and places each “text item” in theText [i.e. the characters between the return characters] appears on its own line.

Thanks, and as always, appreciate the help.

Joel

PS. If someone could come back to me on my icon question above that too would be appreciated.

there are two main operations using text item delimiters
¢ convert a string into a list separating the items by the specified delimiter


set theText to "alpha, beta, gamma"
set {TID, text item delimiters} to {text item delimiters, ", "}
set parameterList to text items of theText
set text item delimiters to TID
parameterList --> {"alpha", "beta", "gamma"}

¢ convert a list into a string joining the items by putting the specified delimiter between each item


set parameterList to {"alpha", "beta", "gamma"}
set {TID, text item delimiters} to {text item delimiters, return}
set theText to parameterList as text
set text item delimiters to TID
theText --> "alpha\rbeta\rgamma"