Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write to a text file using AppleScript?

Tags:

So, that's it. How can I write to a text file using AppleScript?

I've tried googling around, but answers seem to be years old and I'm not really sure what should be the preferred idiom this days.

like image 773
Juan A. Navarro Avatar asked Sep 23 '10 17:09

Juan A. Navarro


People also ask

Can you write scripts on Mac?

Script Editor lets you create powerful scripts, tools, and even apps. You can create scripts to perform repetitive tasks, automate complex workflows, and control apps or even the system.

How do you comment AppleScript?

Alternative Single Line Comments Starting in AppleScript 2.0, support for comments using "#" was added.

What language does AppleScript use?

AppleScript scripts are composed, by motivated users like yourself, in AppleScript, an English-like language containing many of the verbs, nouns, adjectives, articles and other English language elements we use every day.


1 Answers

on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)     try         set the target_file to the target_file as text         set the open_target_file to ¬             open for access file target_file with write permission         if append_data is false then ¬             set eof of the open_target_file to 0         write this_data to the open_target_file starting at eof         close access the open_target_file         return true     on error         try             close access file target_file         end try         return false     end try end write_to_file 

Interfacing with it can be cleaned up with the following...

my WriteLog("Once upon a time in Silicon Valley...")  on WriteLog(the_text)     set this_story to the_text     set this_file to (((path to desktop folder) as text) & "MY STORY")     my write_to_file(this_story, this_file, true) end WriteLog 
like image 105
Philip Regan Avatar answered Sep 17 '22 19:09

Philip Regan