-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/06/13 13:59
# dMod: 2016/06/13 14:14
# Appl: AppleScriptObjC
# Task: Write UTF8 Text to a file – creating the directory path as required.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Write, @UTF8, @Text, @File, @Create, @Make, @Directory, @Folder
-------------------------------------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite & later
use framework "Foundation"
use scripting additions
-------------------------------------------------------------------------------------------
set fileContent to text 2 thru -1 of "
01 Now is the time for all good men to come to the aid of their country.
02 Now is the time for all good men to come to the aid of their country.
"
set fileName to "test file 01.txt"
set newDirPath to POSIX path of ((path to downloads folder as text) & "test01:test02:")
set newFilePath to newDirPath & fileName
its createDirectoryAtPath:newDirPath
its writeString:fileContent toPath:newFilePath
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on createDirectoryAtPath:thePath
set {theResult, theError} to current application's NSFileManager's defaultManager()'s createDirectoryAtPath:thePath withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
if not (theResult as boolean) then
set errorMsg to theError's localizedDescription() as text
error errorMsg
end if
end createDirectoryAtPath:
-------------------------------------------------------------------------------------------
on writeString:aString toPath:posixPath
set anNSString to current application's NSString's stringWithString:aString
anNSString's writeToFile:posixPath atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end writeString:toPath:
# Other encodings: NSMacOSRomanStringEncoding
-------------------------------------------------------------------------------------------