this has no doubt been discussed but...

How can i open a file in text edit and then get the number there, paste it somewhere and then replace the number with n+1.

i can open textedit but can work out how to open a file, its the path i dont get, i am normally a windows user and have only been using mac for 11 days now.

also i think i know how to change the text

set text to number + 1

does that work?? havent checked it as still working through my url problem.

Is there any reason why you need to use Text Edit. AppleScript can read files wihtout needing to use an application to do so.

Pasting it somewhere will depend on the application your trying to paste in. But you probably don’t need to use the clipboard to do that, but that would probably work as well.

A better description of what you are trying would help.

Kevin

well what i would like to do is have a text doc with a number 10 for example.

I need to insert that number into a url string and then update the number by one in thge text folder.

This is so that the text doc can be changed and the script willnot have to be opened.

The text doc will only have 2 lines, each different numbers for different counters.

Better???

OK how about this:

I created a text flle using the TextEdit application called number.txt and I saved it in my documents folder. The text file contained two numbers seperated by a space, simpler than a return because of the confusion between returns and line feeds.


set theFile to (path to current user folder as string) & "Documents:number.txt" as alias
set fileRef to open for access theFile with write permission
set theNums to read fileRef using delimiter " " as integer
set eof fileRef to 0
write (((item 1 of theNums) + 1) as string) & " " & (((item 2 of theNums) + 1) as string) to fileRef
close access fileRef
set theURL1 to "http://www.mywebsite.com/theDocs/document" & item 1 of theNums & ".html"
set theURL2 to "http://www.mywebsite.com/theDocs/picture" & item 2 of theNums & ".jpg"

This both updates your numbers and creates two different urls. I hth.

Kevin

hasnt worked as planned, it opens a text document but not the one that i have saved.

I changed your code after realising that i only needed one number. The code is:


tell application "TextEdit"
	--this opens the imageNumberFile and uses the number.  It then updates the number for the next image.
	--this line opens the file from the desktop
	set imageNumberFile to (path to current user folder as string) & "Desktop:imageNumberFile.rtf" as alias
	--this sets the read and write access
	set fileRef to open for access imageNumberFile with write permission
	--sets the cdNumber with the information
	set imageNumber to read fileRef as integer
	write (((imageNumber) + 1) as string) to fileRef
	--closes th file reference
	close access fileRef
	
end tell

As i mentioned before i dont get the file path as i have only just started using apple mac!

Save this as an application - it will become a droplet.

on open dropped_
	set the clipboard to (item 1 of dropped_) as text
end open

If you ever need the path to an item, drop the item’s icon onto the droplet. Note: It will place the path on your clipboard for easy pasting. If there’s something on your clipboard that you don’t want to lose, don’t use the script.

– Rob

thanks very useful that droplet no doubt i will use that more often.

But for somereason the original code still doesn’t work.

After adding the new path it will open a blank document.

Am i being weird and missing something vital???

the new code is:


tell application "TextEdit"
	--this opens the imageNumberFile and uses the number.  It then updates the number for the next image.
	--this line opens the file from the desktop
	set imageNumberFile to (path to current user folder as string) & "Shared G4 HD:Users:karrenmay:Desktop:imageNumberFile.rtf" as alias
	--this sets the read and write access
	set fileRef to open for access imageNumberFile with write permission
	--sets the cdNumber with the information
	set imageNumber to read fileRef as integer
	write (((imageNumber) + 1) as string) to fileRef
	--closes th file reference
	display dialog imageNumber
	close access fileRef
	
end tell

Are you sure that this line is generating the correct path?

set imageNumberFile to (path to current user folder as string) & "Shared G4 HD:Users:karrenmay:Desktop:imageNumberFile.rtf"

Note: If an existing file isn’t found by the ‘open for access’ command, a new file will be created.

sorry I am in a real hurry so I hope I dont confuse the issue.

First you need to set up textedit via its preferences to save files as text. The rtf part will confuse applescript.

Secondly, if you want the file to be on the desktop you can access anything on the desktop directly with:

path to desktop folder

Thirdly
None of the applescripts commands used need TextEdit, so you dont’ need the tell application “TextEdit” and

end tell

that you have used.

Kevin