Tutorial MAC users - Batch Text Replacement

Discussion in 'Starbound Modding' started by szhlopp, Feb 7, 2014.

  1. szhlopp

    szhlopp Void-Bound Voyager

    Hey guys,

    So here's a quick app I wrote for quickly replacing text in your JSON files. I decided to rename a crafting station and EVERY item that was crafted inside it and there was no way I was going to do that by hand (100+ files). So, I wrote an AppleScript to do it.

    A few example uses
    • Renaming stations, items, updating descriptions...
    • Replacing every sword in your "rare swords folder" with a new damage value.
    • Fixing your mod with a brand new Starbound version. (I'm still debugging/testing the version that has Regular Expressions enabled... more to come on that soon)

    Tutorial:
    1. Copy the code below into a brand new AppleScript document and save it somewhere
    2. BACKUP your entire mods folder before running this script. (don't accidentally ruin your mod)
    3. Press the "Run" button in the Applescript Editor
    4. It will ask you for the parent folder(you can select your mod's folder, or any subfolder underneath it as a starting point)
    5. It will then ask you for an extension to search for. Examples (no quotes): "recipe" or "sword" or "item"
    6. It will ask you about your text replacements
    7. Voila! You've now batch edited your Starbound mod.
    8. Test thoroughly!


    Code is below. Hope this saves someone a lot of time... Enjoy!

    Code:
    --        Starbound JSON renamer
    --        By: Szhlopp
    --        Minimal Development
    
    
    tell application "Finder"
       
       
        display dialog "ALWAYS ALWAYS ALWAYS make a backup of the parent folder before running this script!!!!! I AM NOT RESPONSIBLE FOR YOU CORRUPTING FILES"
       
        set file_list to every file of entire contents of (choose folder with prompt "Select the parent folder")
        --set outputDirectory to choose folder with prompt "Select the output folder"
       
        set fileext to text returned of (display dialog "What file extension should I look for? (NO PERIOD). ex: 'recipe'" default answer "")
        set replacefrom to text returned of (display dialog "What should I replace? (from)" default answer "")
        set replaceto to text returned of (display dialog "What should I set " & replacefrom & " to?" default answer "")
       
       
        repeat with f in file_list
           
            -- get extension
            set l to my splitString(f as text, ".")
           
           
            --Test extension
            if last item of l = fileext then
                set itemName to (item 1 of l)
                set itemContents to read file (f as string)
                --display dialog f as string
               
                if replacefrom is in itemContents then
                    my write_to_file(my replaceText(itemContents, replacefrom, replaceto), (f as string), false)
                end if
               
            end if
           
        end repeat
       
       
    end tell
    
    
    on write_to_file(this_data, target_file, append_data)
        try
            tell application "System Events" to exists file target_file
            if not the result then do shell script "> " & quoted form of target_file
            set the open_target_file to open for access 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 e
            try
                display dialog e
                close access target_file
            end try
            return false
        end try
    end write_to_file
    
    to splitString(aString, delimiter)
        set retVal to {}
        set prevDelimiter to AppleScript's text item delimiters
        log delimiter
        set AppleScript's text item delimiters to {delimiter}
        set retVal to every text item of aString
        set AppleScript's text item delimiters to prevDelimiter
        return retVal
    end splitString
    
    to joinList(aList, delimiter)
        set retVal to ""
        set prevDelimiter to AppleScript's text item delimiters
        set AppleScript's text item delimiters to delimiter
        set retVal to aList as string
        set AppleScript's text item delimiters to prevDelimiter
        return retVal
    end joinList
    
    
    to replaceText(someText, oldItem, newItem)
        (*
        replace all occurances of oldItem with newItem
              parameters -    someText [text]: the text containing the item(s) to change
                        oldItem [text, list of text]: the item to be replaced
                        newItem [text]: the item to replace with
              returns [text]:    the text with the item(s) replaced
        *)
        set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
        try
            set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
            set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
        on error errorMessage number errorNumber -- oops
            set AppleScript's text item delimiters to tempTID
            error errorMessage number errorNumber -- pass it on
        end try
       
        return someText
    end replaceText
     
    The | Suit likes this.

Share This Page