• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
ASObjC – Write File & Create Directory Handlers
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ASObjC – Write File & Create Directory Handlers


  • Subject: ASObjC – Write File & Create Directory Handlers
  • From: Christopher Stone <email@hidden>
  • Date: Mon, 13 Jun 2016 14:26:04 -0500

Hey Folks,

Here's a couple of handlers I've been working on for a project today.

createDirectoryAtPath

Similar to `mkdir -p` it will create intermediate directories as needed.

writeString

Writes text to a path – current encoding UTF8.

With the two handlers together the appended script will write to file and create any directories required to complete the path.

For my own use I generally prefer the Satimage.osax's writetext command, but it won't create directories on-the-fly.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# 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
-------------------------------------------------------------------------------------------

 _______________________________________________
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

  • Prev by Date: Script Actions in Tk Windows?
  • Next by Date: How Do I Create class HTML to put on Clipboard?
  • Previous by thread: Script Actions in Tk Windows?
  • Next by thread: How Do I Create class HTML to put on Clipboard?
  • Index(es):
    • Date
    • Thread