help with applescript on creating a text file

Discussion in 'Mac Programming' started by BruisedGhost, Sep 29, 2008.

  1. BruisedGhost macrumors newbie

    Joined:
    Sep 29, 2008
    #1
    Hi I am very new to Applescript and I am trying to write a script that prompts for a set amount of variables, then outputs them to a txt file and saves the text file. I am having two issues.

    1. I dont know how to add a line break in the text file so everything is coming out as one line

    2. the file is not saving as the type I want and is asking me what I want to save the file as. is there anyway around this?

    attached is my script

    tell application "TextEdit"
    activate
    make new document
    display dialog "please enter the instance name:" default answer "XG"
    set dialogInfo to result
    set selectedButton to button returned of dialogInfo
    set INS to text returned of dialogInfo
    display dialog "please enter the Server name:" default answer ""
    set dialogInfo to result
    set selectedButton to button returned of dialogInfo
    set SERVER to text returned of dialogInfo
    display dialog "please enter Port number:" default answer "1521"
    set dialogInfo to result
    set selectedButton to button returned of dialogInfo
    set PRT to text returned of dialogInfo
    display dialog "please enter the SID of your database:" default answer "XG"
    set dialogInfo to result
    set selectedButton to button returned of dialogInfo
    set SID to text returned of dialogInfo
    display dialog "please enter you mail server:" default answer ""
    set dialogInfo to result
    set selectedButton to button returned of dialogInfo
    set MAILR to text returned of dialogInfo
    set text of document 1 to INS & "_DB=jdbc:eek:racle:thin:mad:" & SERVER & ":" & PRT & ":" & SID & return & "OS=WIN" & return & "SSCE=SSCE\runtime\" & return & "MAIL_SERVER=" & MAILR & return & "MAIL_PORT=25" & return & "MAIL_USE_AUTH=false" & return & "MAIL_USER_SSL=false"as text
    save document 1 in "/Users/Root/Desktop/jfile.txt"
    end tell

    Thanks!
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    As for #1, you are using "return" correctly, so I think the issue could be that you're trying to open the file with an app that is expecting different newline markers, or Text Edit is changing your line endings.

    UNIX and most UNIX variants including Mac OS X --> newline/linefeed (LF or \n)
    Other older OSs --> carriage return (CR or \r)
    Windows, DOS --> carriage return + linefeed (CRLF or \r\n)

    You can try different combinations by setting your own constants:

    Code:
    set crlf to (ASCII character 13) & (ASCII character 10)
    Then use "crlf" where you were using "return". But then if that works for Windows, you might have problems viewing it on another OS, so I really don't recommend this approach unless you have no other option. Better yet, see if the app you're viewing the text file output in can be set to interpret the correct line endings. Most can. It's probably in the Preferences or one of the menus.

    Because you are using AppleScript, it might be a little trickier. It could be that the app you use to enter the text (in your case Text Edit) is reinterpreting the "return" characters as it saves the file. Therefore it may not even matter what newline characters you use to build the text. It may be that you need to set the preferences of the saving application to use the newline characters you want. Try changing those prefs in Text Edit, to for example UTF-8, and see if it makes a difference. I don't know if Text Edit's preferences are themselves scriptable so that this would work reliably on all systems.

    A better approach in your example might be to directly write the text into a file from AppleScript and not use a text editing app at all, since you don't seem to be using it for anything other than a temporary text holder. That may very well also solve your problem #2 where it's asking you for the file type (Text Edit can edit both plain text and Rich Text formats). Try something like this (sorry, I'm not in a place where I can test that now):

    Code:
    set s to "Here's some text I want to save." & return & "Here's another line." & return
    set theFile to open for access file "Users:Root:Desktop:jfile.txt" with write permission
    write s to theFile
    close access theFile
    
     
  3. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    In any Mac text file, all you need is a return.

    If you want to stick with TextEdit, you need to go to Preferences and select that new documents are plain text. I don't believe there's a way to handle this in AS, unless you use System Events. If you want to use a text editor, take a look at Tex-Edit, much better scripting dictionary.

    BTW, you can save yourself a few lines of code. You don't need to save the result into a variable to peel off the data you want. This will work nicely:

    display dialog "please enter the instance name:" default answer "XG"
    set INS to text returned of result

    Applescript also requires "\\" for every backslash. So for this string:

    "SSCE=SSCE\runtime\"

    you need to say:

    "SSCE=SSCE\\runtime\\"

    for it to compile correctly. This could be the root to some of your other problems.

    mt
     

Share This Page