Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript: to open a file, add a line of text at the top, and then close the file.

What I need is a small simple Applescript that will:


(1) Open a textfile called 22 in a folder on the desktop of my Mac called "OUTPUTDESKTOP"


(2) Add a line of text above the existing first line of the file saying "This is file 22" followed by a "Return"


(3) Close the file


I have "TextWrangler" (v3.1) inn the Applications folder of my Mac, and that would be my preferred application to use for this (assuming that an application is required)


Can some kind person please help me with this.


Thanks you

iMac Intel, Mac OS X (10.4.1)

Posted on Jul 7, 2012 3:51 AM

Reply
14 replies
Sort By: 

Jul 7, 2012 4:28 AM in response to Philip Caplan

Assuming the name of your text file is actually "22.txt", although the displayed name might be just "22", the following small simple script should do exactly what you are asking for:


set thePath to (((path to desktop) as text) & "OUTPUTDESKTOP:" & "22.txt")

set N to open for accessthePath with write permission

set theText to read N

set eofNto 0

write "This is file 22" & return & theText to N

close accessN

Reply

Jul 7, 2012 3:11 PM in response to Philip Caplan

I tried the program.


That means the files doesn't exist or the file has no data in the file.


......

set thePath to (((path to desktop) as text) & "OUTPUTDESKTOP:" & "22.txt")


tell application "Finder"

if exists file thePath then

display dialog "the file exists" giving up after 3

else

display dialog "the file " & thePath & " does not exists" giving up after 60

return

end if

end tell



set N to open for access thePath with write permission


set theText to read N


set eof N to 0


write "This is file 22" & return & theText to N


close access N

I had trouble with the Unix cat command displaying the file. (data over written ) What linend does return insert? I quess it's not the unix one.



--------------------------------------------------------------




Robert

Reply

Jul 7, 2012 3:23 PM in response to rccharles

This version has a few more checks in it.


set thePath to (((path to desktop) as text) & "OUTPUTDESKTOP:" & "22.txt")


tell application "Finder"

if exists file thePath then

display dialog "the file exists" giving up after 3

else

display dialog "the file " & thePath & " does not exists" giving up after 60

return

end if

end tell



set N to open for access thePath with write permission

set theSize to (get eof N)

if theSize > 0 then

set theText to read N

else

set theText to ""

end if


set eof N to 0


write "This is file 22" & return & theText to N


close access N

Reply

Jul 7, 2012 4:01 PM in response to rccharles

Many thanks. This is **almost** what I need!


What is wrong is that if the file doesn't exist, I need the script to exit silently (no messages, with the file closed).


And if the file does exist, I need the script to do it's work (adding the line of text at the top) and then close the file and exit silently (no messages).


So, I guess my original description should have been:


(1) LOOK FOR a textfile called 22 in a folder on the desktop of my Mac called "OUTPUTDESKTOP"


(2) If it does **not** exist, end script


(3) If it **does** exist, add a line of text above the existing first line of the file saying "This is file 22" followed by a "Return"


(4) Close the file


Thanks for your help. Looking forward to new script. Kind regards, Philip


PS Regarding the "Return" character, I have uploaded to my website a file which has been run through your script, and it works OK, so the format of the "Return" seems to be correct for my needs, so we don't have to worry about it!

Reply

Jul 7, 2012 10:03 PM in response to Philip Caplan

This new version of the script should do the trick:


try

set theFile to (((path to desktop) as text) & "OUTPUTDESKTOP:" & "22.txt") as alias

set N to open for accesstheFile with write permission

get eofN

if result > 0 then

set theText to read N

set eof N to 0

write "This is file 22" & return & theText to N

end if

close accessN

end try

Reply

Jul 8, 2012 6:32 AM in response to Pierre L.

Sorry, I'm afraid I need a little more help!!!


I am going to be using your script to perform the same action on 50 or more files, and I have no problem with copy-and-pasting it 50 times into one script, and then changing the filename and text by hand 50 times, because the text will in any case be different for every filename.


However, I also wish to put some text (such as <h4>) before each of the texts, and some different text (such as </h4>) after each of the texts, and I would like that to be in 2 variables at the top of the script, so that if I discover either of these "before" or "after" snippets need to be changed, I can make that change in just one place and have it automatically included in all 50 files.


Can you help me by giving the extra code that needs to be added to the script? Many thanks.

Reply

Jul 8, 2012 7:59 AM in response to Philip Caplan

Not sure to perfectly understand what you are asking for.


For example, what should the script do if the text file does exist but is empty. Should the script then just “exit silently” or should it nevertheless write "This is file 22" at the top of the empty text file?


Also, is there some regularity either in the file names (like "22.txt", "23.txt", etc.) or the texts you want to add above their first line (like "This is file 22", "This is file 23", etc.), such that it could be possible to use a repeat loop and avoid “changing the filename and text by hand 50 times”?


Anyway, here's a new version of the script that reflects my understanding of what you are asking for:


set theFolderName to "OUTPUTDESKTOP"

set theFileName to "22.txt"

set theTextAbove to "This is file 22." & return

set theTextBefore to "<h4>" & return

set theTextAfter to "</h4>"


try

set theFile to (((path to desktop) as text) & theFolderName & ":" & theFileName) as alias

set N to open for accesstheFile with write permission

get eofN

if result > 0 then

set theText to (read N) & return

set eof N to 0

writetheTextAbove & theTextBefore & theText & theTextAftertoN

end if

close accessN

end try


Message was edited by: Pierre L.

Reply

Jul 8, 2012 8:17 AM in response to Pierre L.

Thanks, Pierre.


If a file named in the script exists but is empty, the script can still insert text into it.


The following 3 items are the 'fixed' ones, and the way you have them is good:


set theFolderName to "OUTPUTDESKTOP"

set theTextBefore to "<h4>" & return

set theTextAfter to "</h4>"


The other 2 items "theFileName" and "theTextAbove" are to be different for each of the filenames, and as there is **not** a fixed pattern to the filenames, I would **prefer** that I have all of them (there will be an arbitrary number, could be more or less than 50) appearing separately through the script, so that I can edit each one as needed.

Reply

Jul 8, 2012 9:32 AM in response to Philip Caplan

My last suggestion:



display dialog "Name of the text file to modify?" default answer "22" buttons {"Cancel", "OK"} default button 2 with icon 1 with title "OUTPUTDESKTOP"

set theFileName to text returned of result & ".txt"


display dialog "Text to enter at top?" default answer "This is file 22" buttons {"Cancel", "OK"} default button 2 with icon 1 with title theFileName

set theTextAbove to text returned of result & return


try

set theFile to ((path to desktop) as text) & "OUTPUTDESKTOP:" & theFileName as alias

set N to open for accesstheFile with write permission

if (get eof N) > 0 then

set theText to return & (read N) & return

else

set theText to return

end if

set eofNto 0

write theTextAbove & "<h4>" & theText & "</h4>" to N

close accessN

end try

Reply

Applescript: to open a file, add a line of text at the top, and then close the file.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.