0

I am trying to combine two text files text in one text file in some specific manner. I have many text files so I want to automate this process. I trying to learn apple script for this.

How to achieve this with apple script.

Treat it as a two files and wanted to make a new file like this - :

textfile_One.rtf or textfile_One.txt
{
   Hello World
}

textfile_Two.rtf or textfile_Two.txt
{
   Hey Dunia
}

textfile_Three.rtf or textfile_Three.txt
{
   Hello World
   Hey Dunia
}

this kind of output of files

Vikram Sinha
  • 581
  • 1
  • 10
  • 25
  • The answer depends entirely on what the specific manner is - so you will need to be much more forthcoming! – Mark Setchell Jun 14 '17 at 14:47
  • So we write the first line of the first file and the first line of the second file after it into the third file. How do we deduce the names of the first file, the second file and the output file? – Mark Setchell Jun 14 '17 at 16:38
  • Show your approach – Pat_Morita Jun 14 '17 at 16:40
  • I have no idea how I am going to achieve this. but I am working on it; your little guidance would work. – Vikram Sinha Jun 14 '17 at 17:40
  • Here's a quick example using AS `do shell script` command, the basic unix command `cat` and the unix output redirect `>` : `do shell script "cat 'first/file/path.txt' 'second/file/path.txt'>'third/file/path.txt'"` You should know about `quoted form of` in AS to cover for weird characters in file paths, but I've included single quotes in this example. If you don't understand that, do the research online (like stackoverflow) – CRGreen Jun 14 '17 at 18:20
  • This is a very basic AppleScript function. It's specifically addressed in documentation and accessible in a simple google search. If after reading and following the example you're still confused, post what you've tried and where you're having troubles. Here's the specific documentation. https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html –  Jun 15 '17 at 03:42
  • Thanks @Chilly let me go through with https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html – Vikram Sinha Jun 15 '17 at 08:04

1 Answers1

0
tell application "TextEdit"
    activate
    set source_doc to open file "source 1.txt"
    set source_doc2 to open file "source 2.txt"
    set destination_doc to make new document
    delay 1
    make new paragraph ¬
        at end of every paragraph of text of destination_doc
    duplicate every paragraph of text of source_doc ¬
        to end of every paragraph of text of destination_doc
    duplicate every paragraph of text of source_doc2 ¬
        to end of every paragraph of text of destination_doc
end tell

OR

tell application "TextEdit"
    activate
    (choose file) as string
    set source_doc to the result
    delay 0.2
    (choose file) as string
    set source_doc2 to the result
    delay 0.2
    set source_doc to open file source_doc
    set source_doc2 to open file source_doc2
    set destination_doc to make new document with properties {name:"combined", name extension:"txt"}
    delay 0.2
    make new paragraph ¬
        at end of every paragraph of text of destination_doc
    delay 0.2
    duplicate every paragraph of text of source_doc ¬
        to end of every paragraph of text of destination_doc
    delay 0.2
    duplicate every paragraph of text of source_doc2 ¬
        to end of every paragraph of text of destination_doc
end tell
wch1zpink
  • 3,026
  • 1
  • 8
  • 19