Re: Problem saving a new file
Re: Problem saving a new file
- Subject: Re: Problem saving a new file
- From: Eric Robertson <email@hidden>
- Date: Sat, 20 Oct 2012 17:30:58 +0100
Le 19/10/2012 à 15:54, Eric Robertson < email@hidden> a écrit : I'm a newbie and I'm having problems saving some data away to a new file. I've tried various things but the nearest I've got to doing this was the following:
tell application "TextEdit" set newFile to "/Users/ericrobertson/Documents/New Notes.rtf" activate make new document with properties {path:newFile} set text of document "New Notes.rtf" to "This is sample text" save document "New Notes.rtf" end tell
However although the document appears OK inTextEdit with the text included in it I get the message that I don't own the document "New Notes.rtf" and don't have permission to write to it. Do I have to set the file permissions in some way? I did try setting the name property as well as the path but this made no difference as it seemed to derive the name from just the path.
Incidentally i did find that if I just set the name property the path was set to /Users/ericrobertson/Library/Containers/com.apple.TextEdit/Data/Users/ericrobertson/Documents/New Notes.rtf.
I'm still on Lion (10.7.5) using TextEdit v1.7.1.
You may use :
set p2doc to path to documents folder as text set newName to "New Note.rtf" tell application "System Events" make new file at end of folder p2doc with properties {name:newName} end tell set newFile to (p2doc & newName) as alias tell application "TextEdit" activate make new document with properties {name:newName} set text of document 1 to "This is sample text" save document 1 in newFile end tell
tested with TextEdit 1.8 (301) under 10.8.2
Yvan KOENIG (VALLAURIS, France) vendredi 19 octobre 2012 18:09:26 That's grand Yvan, many thanks. It works OK with my setup too.
I was aware that you can't use a file alias for a non-existent one but I thought I'd read that while the dictionary of some applications specified that a file alias had to be used you could get by without using one. This doesn't work here and I see how you've got over this problem.
I was really wanting to save the file in a new folder in Documents called Notes. I could just create that new folder in the normal way but I wondered how to do this in the script. I have managed this with
set p2doc to (path to documents folder as text) set newFolderName to "Notes" set newFolder to (path to documents folder as text) & newFolderName & ":" set newName to "New Note.rtf" tell application "System Events" make new folder in folder p2doc with properties {name:newFolderName} make new file at end of folder newFolder with properties {name:newName} end tell
I've written a lot of Perl and _javascript_ and I've found that the cookbooks for these are invaluable for working out how to solve a problem. I've been reading "AppleScript, The Definitive Guide" and "AppleScript 1-2-3" but I couldn't find in either of these a solution to my problem. Do you know of another book that is more in the nature of a cookbook?
I've been following this list for some time now and it seems that very few postings are by newbies like myself. Is it OK for me to post questions like this to this list or is the MacScripter forum a more appropriate place?
Eric |
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden