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

Save a Text Edit document to a path with variables in Applescript

Hello, I'm working on a script to transfer data from my camera's memory card to my hard drive automatically. Basically I'm making my own version of the popular app ShotPut Pro because I don't want to pay for it! I have the entire script done and it works beautifully except for saving a log file. I am able to get my log constructed in a TextEdit document but I am having all kinds of trouble saving the file. I copied the part of the script responsible for logging below so you can see the problem. I'll do the best I can to explain the process.


The data is copied in 4 stages. First, the script creates a folder with today's date and a user inputted name (for example, a folder could be named "2012_7_1 (Summer Vacation)". After the folder is created, the script duplicates the "100CANON" folder from the memory card into the new dated and named folder on my hard drive. Once the duplication is complete, it verifies the copy by comparing the size of the original folder and the copied folder. In the final step, the script asks if I want to reformat my memory card (deleting the 100CANON folder, enptying the trash, and recreating a new 100CANON folder) and eject it or just leave it with the files on. After each stpe, a corresponding variable is given a number 0 or 1 depending on the option chosen.


When the log is created, it simple puts together the 4 log entry variables and sets that as the text in a new text edit document. Now I need to save the document in the folder I created (the "2012_7_1 (Summer Vacation)" folder). In the last line of the script, you see the variable "fname". That is the Folder Name variable that was defined by the user and it is the same variable that was used to create the folder.


Now for the problem. It saves the log in the root of "Macintosh HD". What's even more strange is that the name of the text file is the path I want it to save to. I've tried every variation I can think of, resulting in errors telling my I cannot save the file with the format "log" (I tried to make the file named log when I saved) and things like that. I also tried doing the entire save process using system events keystroked only to realize I could not find a way to navigate to the right colfer using the keystrokes only. Please help me streamline this and get the files to save in the right folder, thanks!


Here is the excerpt of the script:




if cgood = 1 then set entry0 to "Successful transfer. "

if cgood = 0 then set entry0 to "Failed transfer. "


if samename = 0 then set entry1 to " The folder was given a unique name. "

if samename = 1 then set entry1 to " The folder was given a name that already existed. The name was then changed by the user to a unique name. "


if slasherror = 0 then set entry2 to "The folder was given a name that does not contain /. "

if slasherror = 1 then set entry2 to "The folder was given a name that contained /. The folder was renamed by the user so that it did not contain /. "


if frmtd = 0 then set entry3 to "The card was not formatted or automatically ejected."

if frmtd = 1 then set entry3 to "The card was automatically formatted then ejected."


set logtype to (current date) & fname & entry0 & entry1 & entry2 & entry3


tell application "TextEdit"


activate


makenewdocument

set text of document 1 to logtype as text

save document 1 in file ("Macintosh HD/Users/StevePorter/Pictures/Canon EOS T1i/" & fname)

end tell

iMac, Mac OS X (10.6.8)

Posted on Jul 1, 2012 2:19 PM

Reply
5 replies

Jul 1, 2012 3:00 PM in response to TacosAreDelicious

I think your problem is with your file path specification. it should be:


"/Users/StevePorter/Pictures/Canon EOS T1i/" & fname


Posix (slash-delimited) file paths don't include the root directory; Mac (colon-delimited) file paths do.


Also, there's no need to use TextEdit at all in this case. you can write the file directly from applescript like so:


set fp to open for access "/Users/StevePorter/Pictures/Canon EOS T1i/" & fname with write permission

set eoffpto 0

writelogtypetofp

close accessfp


if you're adding lines to an ongoing log file rather than writing a new file for each log, delete the 'set eof' line. that line sets end-of-file to 0, effectively deleting previous text.

Jul 1, 2012 3:48 PM in response to twtwtw

twtwtw,


I tried removing "Macintosh HD" from the save path but it still saved to the root of Mac HD like it did before, and the file is named name does not have "Macintosh HD" in it. Same thing as before just a different file name.


Then I tried the code you provided (I put the entire tell for TextEdit as a commend then copied yours in its place) and got an error saying "An error of type -1409 has occured." I did some googling of error type -1409 but got nothing close to usable. I'm interested though about creating a file from directly within Applescript, I get how the code you wrote works but what exacty is the 'fp' variable?

Jul 1, 2012 4:16 PM in response to TacosAreDelicious

Try this:


set logtype to 12345 -- some number for example

set fileName to "My log file"

set fname to "2012_7_1 (Summer Vacation)"

set folderPath to "/Users/StevePorter/Pictures/Canon EOS T1i/" & fname

set theFile to POSIX file (folderPath & "/" & fileName) -- that file doesn't exist yet


set N to open for accesstheFile with write permission-- creates the text file

set eofNto 0

write (logtype as text) to N

close accessN


tell application "TextEdit" to open theFile


N is just a reference number to the text file.

Jul 1, 2012 4:29 PM in response to TacosAreDelicious

Well, TextEdit's Applescript implementation ahs always been a little flakey...


'fp' is just my personal shorthand for 'file pointer'. that's all it is - a reference to the file that got opened to make things easier.


error -1409 means nothing to me, either. it might just be gibberish. The common errors with open for access are badly constructed file paths and files that are already open somewhere else. Many apps (and applescript itself) lock the file when they open it to prevent external modifications. if you test a script and it crashes the file pointer may still be open, so open a new editor window and run the command close access "/Users/StevePorter/Pictures/Canon EOS T1i/..." (hard-code the file name - the second window won't have access to the fname variable).

Jul 1, 2012 4:57 PM in response to twtwtw

Alright, thanks for your help! I just did a temporary workaround of adding a line to move the log file from Macintosh HD to the appropriate location and it works, I'm going to look into redoing the whole thing from scratch a bit more cleanly this summer. I'll do some research about writing the file right inside of the script, hopefully I can get this thing to work right. This will do for now, it does what I need it to do and it's clean enough. Thanks again!

Save a Text Edit document to a path with variables in Applescript

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