File from Desktop to Diff (Established) Folder

My problem: Putting a (text) file I create into a folder that is not one of the “path to” constants.


try
	tell me to activate
	set saveFile to button returned of (display dialog "Do you want to save this page?" buttons {"Yes", "No"} default button 2 with icon note)
	if saveFile = "Yes" then
		set title to text returned of (display dialog "Enter title for text: " default answer "")
		set title to title & ".txt"
		tell application "Firefox"
			activate
		end tell
		tell application "System Events" to keystroke "a" using {command down}
		tell application "System Events" to keystroke "c" using {command down}
		tell me to activate
		set selecTxt to the clipboard as text
		tell application "Firefox" to activate
		set dat to (current date) as text
		*--set clipFil to (path to desktop folder as text) & title
		*set clipFil to (path to "Macintosh HD:Users:sw:Desktop:writing" as text) & title
		try
			close access file clipFil
		end try
		set filRef to open for access file clipFil with write permission
		write (dat & return & selecTxt & return & "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & return & return) to filRef starting at eof
		close access filRef
	end if
end try

The starred lines are the ones I’m working with. I’ve looked at POSIX FAQs too, but I don’t understand how to apply them. The first line works, but I don’t want the files I’m creating to go to the Desktop. I want them to go to the folder in Desktop called “writing.”

Extras:
So, intro post for me. I just started on Applescript today. I have prior programming experience in other languages though. This code is a mish mash of other code examples I’ve been looking at most of the day. I like to read a lot of online writing and I was trying to create something that would make it so I don’t have to “select(long sections)-copy-paste-save” over and over. And this is for very specific formatting right now, but if I can understand how to do this saving, I can definitely apply it to other things.

Thank you!

Model: MacBook Pro
AppleScript: 2.3
Browser: Firefox 5.0
Operating System: Mac OS X (10.6)

The approach is you create a string which represents the path to the file. We know how to add strings together which is the reason we work with strings. Then you convert that string into a file reference so you can use it with an applescript command. So to perform your task I would convert the “path to desktop” file reference into a string, then add the rest of the string to it so I have the proper path. Here’s how…

set desktopFolderPath to path to desktop as text
set folderName to "writing"
set fileName to "test.txt"

set folderPath to desktopFolderPath & folderName & ":"
set filePath to folderPath & fileName

-- make sure the folder exists
tell application "Finder"
	if not (exists folder folderPath) then
		make new folder at folder desktopFolderPath with properties {name:folderName}
	end if
end tell

-- write the file
set fileContents to "some text I want in the file"
set openFile to open for access file filePath with write permission
write fileContents to openFile as text
close access openFile

That works perfectly. Thank you so much for helping me out with this. I can’t wait to do more! :slight_smile:

Glad to help! Notice that when I used one of the string paths, I put the word folder in front of the folder path and the word file in front of the file path. That’s how you convert the string path into the appropriate type used with applescript. Good luck. :smiley: