It is so much easier to save a new TextEdit document, in order to create it. I figure you have had a look into the dictionary by now, and just as an exercise, figure out how to save a TextEdit document by supplying a path and name.
It is cheating to look before you have tried.
tell application "TextEdit"
tell (make new document at front)
# returns a window as an object, saves one line.
# make new document at front ...
# tell document 1
# or alternatively referencing document 1 through the snippet...
set its text to "This is a drill."
save it in file (((path to desktop folder) as text) & "newtext.txt")
end tell
end tell
To McUsrII: Thanks for the help. Yes, I did try, a lot, before I read your answer. It works, and even lets TextEdit create an RTF file, which is what I really wanted.
However, I also wanted to create and save a new file with a different application, PDFpenPro. Here the same syntax just doesn’t work. Maybe the problem is with the application, I may write the developer. (Meanwhile, I used a workaround: I used GUI-scripting and programmed a command-D to put the file on the desktop; or I could have created a blank file beforehand and copied it to the desktop, instead of creating it anew, or…)
DJ Bazzie Wazzie: Using Open For Access doesn’t work, because the problem isn’t creation (that’s easy) but saving; in addition, no PDF editor or RFT editor that I know of will open a blank file.
Model: MacBook Pro
AppleScript: 2.5.1
Browser: Firefox 22.0
Operating System: Mac OS X (10.8)
More on this: I’ve succeeded in creating a simple script to make and save a simple RTF file using TextEdit. However, the same script, changing only the application’s name and the file’s suffix, does not work with PDFpenPro. I’ve written to PDFpenPro’s developers. Script follows:
set d to (path to desktop) as text
tell application “TextEdit”
activate
make document
save window 1 in (d & “a.rtf”)
end tell
tell application “PDFpenPro”
activate
make document
save window 1 in (d & “a.pdf”)
end tell
What I’ve done in the past is create blank template files for the rtf or pdf. Then you can just duplicate the templates. You now have files with correct bom. Not sure if open for access can create rtf with proper bom.
gl,
kel
Model: MBP
AppleScript: AS 2.2.4
Browser: Safari 536.28.10
Operating System: Mac OS X (10.8)
Yes, clearly I could create blank templates and copy them, but some strange obsessive-compulsive issue is pushing me to press on with file creation. I’ve already gotten a response from PDFpenPro’s tech support, but it needs further elaboration. I’ll fill in the details when I know them.
Definitely, open for access doesn’t work.
(What is “bom”, BTW?)
Jonathan
Model: MacBook Pro
AppleScript: 2.5.1
Browser: Firefox 22.0
Operating System: Mac OS X (10.8)
set the_data to «data rdat7B5C727466315C616E73695C616E7369637067313235325C636F636F61727466313138375C636F636F617375627274663339300A7B5C666F6E7474626C7D0A7B5C636F6C6F7274626C3B5C7265643235355C677265656E3235355C626C75653235353B7D0A5C6D6172676C313434305C6D61726772313434305C766965777731303830305C7669657768383430305C766965776B696E64300A7D»
set f to choose file name
set ref_num to open for access f with write permission
try
write the_data to ref_num as data
close access ref_num
on error err_msg
close access ref_num
display dialog err_msg
end try
The data is taken from a blank TextEdit rtf. Still needs testing on the file to see if is 100% rtf.
Actually I’m not sure if the data is the bom, but I call it that. It just gives info on the type of text file, otherwise an application opening a file won’t know what kind of file it is.
I did the same thing with a blank pdf and it seems to be working:
set the_data to «data rdat255044462D312E330A25C4E5F2E5EBA7F3A0D0C4C60A342030206F626A0A3C3C202F4C656E677468203520302052202F46696C746572202F466C6174654465636F6465203E3E0A73747265616D0A78012B5408040001E700E30A656E6473747265616D0A656E646F626A0A352030206F626A0A31310A656E646F626A0A322030206F626A0A3C3C202F54797065202F50616765202F506172656E74203320302052202F5265736F7572636573203620302052202F436F6E74656E7473203420302052202F4D65646961426F78205B30203020363132203739325D0A3E3E0A656E646F626A0A362030206F626A0A3C3C202F50726F63536574205B202F504446205D203E3E0A656E646F626A0A332030206F626A0A3C3C202F54797065202F5061676573202F4D65646961426F78205B30203020363132203739325D202F436F756E742031202F4B696473205B203220302052205D203E3E0A656E646F626A0A372030206F626A0A3C3C202F54797065202F436174616C6F67202F5061676573203320302052203E3E0A656E646F626A0A382030206F626A0A28556E7469746C6564290A656E646F626A0A392030206F626A0A284D6163204F5320582031302E382E342051756172747A20504446436F6E74657874290A656E646F626A0A31302030206F626A0A284B656C76696E2048616B6B6569290A656E646F626A0A31312030206F626A0A285465787445646974290A656E646F626A0A31322030206F626A0A28443A32303133303730393033323232325A303027303027290A656E646F626A0A31332030206F626A0A28290A656E646F626A0A31342030206F626A0A5B205D0A656E646F626A0A312030206F626A0A3C3C202F5469746C65203820302052202F417574686F7220313020302052202F50726F6475636572203920302052202F43726561746F7220313120302052202F4372656174696F6E446174650A313220302052202F4D6F644461746520313220302052202F4B6579776F72647320313320302052202F4141504C3A4B6579776F72647320313420302052203E3E0A656E646F626A0A787265660A302031350A303030303030303030302036353533352066200A30303030303030363137203030303030206E200A30303030303030313235203030303030206E200A30303030303030323638203030303030206E200A30303030303030303232203030303030206E200A30303030303030313037203030303030206E200A30303030303030323239203030303030206E200A30303030303030333531203030303030206E200A30303030303030343030203030303030206E200A30303030303030343236203030303030206E200A30303030303030343737203030303030206E200A30303030303030353039203030303030206E200A30303030303030353336203030303030206E200A30303030303030353738203030303030206E200A30303030303030353937203030303030206E200A747261696C65720A3C3C202F53697A65203135202F526F6F74203720302052202F496E666F203120302052202F4944205B203C35303033373936383864353166656537366332326539336435363838646231363E0A3C35303033373936383864353166656537366332326539336435363838646231363E205D203E3E0A7374617274787265660A3737340A2525454F460A»
set f to choose file name
set ref_num to open for access f with write permission
try
write the_data to ref_num as data
close access ref_num
on error err_msg
close access ref_num
display dialog err_msg
end try
I exported a blank pdf from TextEdit and read the “bom” data.
It was in addition to your create question rather than save.
boms are byte order marks who indicates the endianness of a file, for UTF-8 it only indicates that it UTF-8 encoded because UTF-8 doesn’t have different endianness. A single byte can only hold up to 255 characters but 2 bytes can hold up to 65535 characters. The problem is that some architectures read from left to right and other architectures reads right from left, meaning that large multi byte numbers are stored differently on Big Endian machines than on Little Endian machines (by default). A bom indicates endianness of the data, boms are commonly used in Unicode character encodings.
Bom is also short for bill of materials, which is possibly the interpretation, when saying that the first 256 bytes of an image is its bom, those bytes would then describe the image format, size, pixel, pallete, etc.
Picture files are commonly streams because they need to be read from the beginning till the end. JPEG, officially, doesn’t have an header, however the first 9 bytes indicates basic file format. BMP and PNG, for example, does have headers, theyr aren’t named bom. TIFF files on the order hand begins with an BOM, but again this is an byte order mark.
Then in PDF the “header” with title, version, creator etc… is also not called an bom but document information dictionary.
This is term, afaik, is only used for the BOMArchiver in Mac OS X, not for images.
You are right about that, aside from bomARchiever, and pkgutil, I don’t know of other stuff on OS X that uses this, the definition below, points in the direction, that a bill of materials is stored externally, and not embedded in a file, nor describing the contents of a file, more describing which files and versions thereof needed to build/install or were used in an installation of a product.
It’s perfectly ridiculous because as nothing was saved, there is no file whose permissions may be changed.
I was forced to edit one instruction as :
save window 1 in file (d & “a.rtf”)
From my point of view, this prove that the error message (which is not reported to the script) is misleading.
The problem is not a permission problem but a coding error.
The dictionary claims :
save‚v : Save an object.
save specifier : the object for the command
[as text] : The file type in which to save the data.
[in alias] : The file in which to save the object.
Passing a string as file reference is clearly wrong.
On the other hand, an alias isn’t really needed.
A simple file reference is sufficient.
It’s fine because there is no need to create the item in order to get an alias to be able to save.
KOENIG Yvan (VALLAURIS, France) jeudi 11 juillet 2013 14:51:45
I prefer to work with documents and not with windows:
set d to (path to desktop) as text
tell application "TextEdit"
activate
set theDoc to make document
#note theDoc reference is broken after the command below
save theDoc in (d & "a.rtf")
end tell