Writing data variable to a file (AS and Xcode)

Hi,
I am using AS and Xcode. I want to write a file to a specific folder (tmp/Scripts/). If I have data that was stored in a variable like theNotes how could I write a file to my desired folder (on any computer)?

In AS, it would be something like the script below. Right now, it is going to the Desktop, but I want this file hidden (tmp/scripts) because it will have data that the program will read back into it upon restart.



set filePath to (path to desktop as string) & "testFile.txt" --Presently this is only going to the desktop.  
try
	set fileRef to open for access file filePath with write permission
	set newText to ¬
	"This is a test file"	
	write newText to fileRef starting at eof
	close access fileRef
on error
	try
		close access file filePath
	end try
end try


Thank you so much!

What kind of data is this, and is it a little or a lot? If it’s not too much, you might want to store it in the user defaults instead of temp/scripts.

Ric

Hi Ric,
Thank you for the reply. Ok basically, I am trying to figure out how do I create this type of AS (below) and save the data (in newText) to a file so I can schedule it in iCal? The problem is that instead of saving this file to the desktop (where it could be deleted) I want to save this to the (usr or tmp file). Then, I want to do this in Xcode.



set calendarName to "Live Events"
set emailAds to "info@email.com"
set Current to current date
set eName to "Test"
set theNotes to "The notes"
set theLocation to "The location"
set theDate to date "Thursday, June 30, 2011 5:00:00 PM"
set theLength to "60"


-- These are the properties for iCal


set subjectMsg to "Live Event Reminder: " & eName & " " & theDate
log subjectMsg

--Writing out the script and saving it to a folder
--The mailing application below will write the script that will mail out the data.  
--THIS IS WHERE I AM HAVING TROUBLE SCHEDULING to tmp
set fileLocation to "/tmp/iCalscripts/"
set theFinalName to eName & ".scpt"
set filePath to fileLocation & theFinalName
log filePath
--set filePath to (path to desktop as string) & eName & ".scpt" --Presently this is only going to the desktop.  

try
	set fileRef to open for access file filePath with write permission
--THIS IS THE PLACE WHERE I WANT THIS DATA TO BE WRITTEN TO A FILE IN (USR or TMP)
set newText to ¬
		"set Mailing to " & "\"" & emailAds & "\"" & return & ¬
		"tell application \"Mail\"" & return & ¬
		"set newMessage to make new outgoing message" & return & ¬
		"tell newMessage" & return & ¬
		"(make new to recipient) make new to recipient with properties" & " {address:Mailing}" & return & ¬
		"set subject of newMessage to " & "\"" & subjectMsg & "\"" & return & ¬
		"set content of newMessage to " & "\"You are receiving this e-mail to remind you of: " & " " & eName & return & return & theNotes & "\"" & return & ¬
		"end tell" & return & ¬
		"send newMessage" & return & ¬
		"end tell"
	
	write newText to fileRef starting at eof
	close access fileRef
on error
	try
		close access file filePath
	end try
end try

set filePath1 to POSIX path of filePath


You can use AppleScript’s “path to temporary items”. Otherwise, your code will work fine in ASObjC, with one exception: you can’t use “file” as a specifier like that. Have a look here for full details: http://www.macosxautomation.com/applescript/apps/gotchas.html

Hi Shane,
You had said use “Path to Temporay Items.” This works fine, except it must flush itself every couple of days because my scripts then get deleted and I run into an error as it looks for them. Would there be another "Path to " that would be hidden from the user?

I found this, but was looking for the entire list of “Path to…”

http://face.centosprime.com/macosxw/applescript-path-to/

Thank you!

Hi Shane,
I found them in Steve Kochan’s Applescript book. There are quite a few and if anyone needs, I can write them in for you.

I am sure there is a way to customize this but I have not figured it out as of yet.

Thanks again,

Yes – that’s why it’s called “temporary”.

Set up your own folder in ~/Library/Application Support/