Problem having textedit open a file

When i run:

tell application "TextEdit"
	open (file "/Users/clegge/Desktop/test.txt")
end tell

It tells me that the file does not exist…

when i type

in a terminal
, i can edit the document…

Pls advise…

thanks!

Hi huggy,

AppleScript uses colon separated paths starting with the volume name like “Mac HD:Users:clegge:Desktop:test.txt”
but there is a “shortcut” to the desktop

tell application "TextEdit"
	open ((path to desktop as Unicode text) & "test.txt")
end tell

Only the shell (and also the do shell script command) uses POSIX paths

fantastic - that was too easy…

thanks!

how would i add a variable to the end of that?


open alias "System:Users:clegge:Desktop:test:pages:" & aFile

aFile is a file variable i have set earlier…

thanks

if aFile is only a filename (string), add parentheses

open alias ("System:Users:clegge:Desktop:test:pages:" & aFile)

if aFile is a file specifier, then it includes the path

open aFile

but this also depends on the expected class of the targetted application

() are your friend

	open ("Macintosh HD:Users:jnierodzik:Desktop:" & afile)

Edit

@stefan : grr :smiley:

that worked - thanks again…

could your recommend a good apple script book for me?

much appreciated…

For absolute starters: AppleScript for Absolute Starters (Free Download)

and my favorite books:
Hanaan Rosenthal - AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Second Edition
Matt Neuburg - AppleScript: The Definitive Guide: Scripting and Automating Your Mac: 2nd Edition (Paperback

downloading them now…

i really appreciate the help…

here is my code so far…


-- lets get a list of the files
tell application "Finder"
	set theFiles to files of (choose folder)
end tell

repeat with aFile in theFiles
	-- open text edit
	tell application "TextEdit"
		--open ((path to desktop as Unicode text) & "test" & "pages" & aFile) -- make the change
		open aFile
		tell application "TextEdit"
			tell text of the front document
				set (every word where it is "Smith") to "Legge"
				beep
			end tell
		end tell
	end tell
end repeat

works great on text files, i would like to be able to do the same thing on HTM files? I know i am missing something here…

One way to do this is using AppleScript’s text item delimiter, e.g.

tell application "Finder" to set theFiles to files of (choose folder)

repeat with aFile in theFiles
	tell application "TextEdit"
		open aFile
		tell document 1
			set theText to its text
			set newText to my replace(theText, "Smith", "Legge")
			set its text to newText
			beep
		end tell
	end tell
end repeat

on replace(txt, srch, repl)
	set {TID, text item delimiters} to {text item delimiters, srch}
	set txt to text items of txt
	set text item delimiters to repl
	set txt to txt as Unicode text
	set text item delimiters to TID
	return txt
end replace

thanks Stefan - i will try this out… Much appreciated…

I was WAAAAAAAAAYYYYYYYYYYYYY off…

And since we are doing a search & replace in TextEdit that means it’s my cue to ask why we are using TextEdit in the first place since AS is more than capable to do all this on it’s own.

tell application "Finder" to set theFiles to files of (choose folder) as alias list

repeat with aFile in theFiles
	set fileRef to (open for access aFile with write permission)
	set txt to read fileRef
	set eof fileRef to 0
	write (replace(txt, "Smith", "Legge")) to fileRef
	close access fileRef
end repeat

on replace(txt, srch, repl)
	set {TID, text item delimiters} to {text item delimiters, srch}
	set txt to text items of txt
	set text item delimiters to repl
	set txt to txt as Unicode text
	set text item delimiters to TID
	return txt
end replace

i thought using text edit would make it easier for a noob such as myself…

the script works fantastically…

i just ordered:
http://www.amazon.com/exec/obidos/ASIN/1590596536/bbsapplescrine0e

and i downloaded the pdf as well…

THANKS AGAIN…

the finished product… Needed this becuase i used a frameset to display an adobe image gallery… The script allows the photographer, who does not know html to basically run the photo gallery util then run the script. then he can drop the folder on my web server and we are LIVE…

MUCH THANKS TO STEFAN AND JAMES…


-- part one will fix all the pages generated by the adobe gallery
-- warning message
display dialog ("After clicking the ok button, please select the PAGES folder of the gallery we will be fixing. PLEASE DO NOT TOUCH ANYTHING WHILE THE SCRIPT IS RUNNING. All Hail StefanK! ") buttons {"OK"}

-- choose the folder
tell application "Finder" to set theFiles to files of (choose folder)
set fileListCount to the length of theFiles
set fileListCountString to fileListCount as Unicode text


-- set our counter
set counter to 0

-- make our changes
repeat with aFile in theFiles
	tell application "TextEdit"
		open aFile
		tell document 1
			set theText to its text
			set newText to my replace(theText, "top.BottomFrame", "top.Content.BottomFrame")
			set its text to newText
			beep
		end tell
		save document 1
		close document 1
	end tell
	set counter to counter + 1
end repeat

on replace(txt, srch, repl)
	set {TID, text item delimiters} to {text item delimiters, srch}
	set txt to text items of txt
	set text item delimiters to repl
	set txt to txt as Unicode text
	set text item delimiters to TID
	return txt
end replace



-- part 2: will fix the thumbnail file 
-- warning message

-- set our counter dialog
set myDiag to fileListCountString & " of " & counter & " files changed."

display dialog myDiag buttons {"CONTINUE"}
display dialog ("After clicking the ok button, please select the THUMBNAILFRAME file of the gallery we will be fixing. All Hail StefanK!") buttons {"OK"}

-- choose the folder
tell application "Finder" to set aFile to (choose file)


-- make our changes
tell application "TextEdit"
	open aFile
	tell document 1
		set theText to its text
		set newText to my replace(theText, "top.TopFrame.document", "top.Content.TopFrame.document")
		set its text to newText
		beep
	end tell
	save document 1
	close document 1
end tell

-- WE ARE DONE
display dialog ("Your changes have been made. You must change the INDEX file for the parent of this gallery for changes to actually work. All Hail StefanK!") buttons {"OK"}