tphilli
tphilli

Reputation: 113

Find and replace AppleScript not writing file

I'm trying to make an AppleScript for the first time in order to change all the event titles in an ics file quickly. I'm using Automator to do this with a simple "Run AppleScript" service.

When I right click the ics file and activate the service, it runs without changing anything. When I run it from Automator it just returns false, which I assume means it failed to write to the file.

on run {inputfile}
    set filedata to inputfile as string
    set filedata to replaceText("Title 1", "Title 2", filedata)
    writeTextToFile(filedata, inputfile, true)
end run

on replaceText(find, replace, textString)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to find
    set textString to text items of textString
    set AppleScript's text item delimiters to replace
    set textString to "" & textString
    set AppleScript's text item delimiters to prevTIDs
    return textString
end replaceText

on writeTextToFile(theText, theFile, overwriteExistingContent)
    try
        
        -- Convert the file to a string
        set theFile to theFile as string
        
        -- Clear the file if content should be overwritten
        if overwriteExistingContent is true then set eof of theOpenedFile to 0
        
        -- Write the new content to the file
        write theText to theOpenedFile starting at eof
        
        -- Close the file
        close access theOpenedFile
        
        -- Return a boolean indicating that writing was successful
        return true
        
        -- Handle a write error
    on error
        
        -- Close the file
        try
            close access file theFile
        end try
        
        -- Return a boolean indicating that writing failed
        return false
    end try
end writeTextToFile

Is it Line 2 where the problem is? I thought you don't need to specify the file path since the service should be inputting the file.

Upvotes: 1

Views: 68

Answers (1)

user3439894
user3439894

Reputation: 7555

Using macOS High Sierra with the Automator Service set to Receives selected [files or folders] in [Finder] and a single Run AppleScript action, the following example AppleScript code works for me to modify an ICS file:

  • Note that as coded it can handle multiple selected files and only valid ICS files can be modified.
on run {input, parameters}
    repeat with anItem in input
        set thisFile to anItem
        if (get {folder, kind} of (info for thisFile)) is {false, "ICS file"} then
            set fileData to (read thisFile)
            set newFileData to replaceText("Title 1", "Title 2", fileData)
            writeTextToFile(newFileData, thisFile, true)
        end if
    end repeat
end run

on replaceText(find, replace, textString)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to find
    set textString to text items of textString
    set AppleScript's text item delimiters to replace
    set textString to "" & textString
    set AppleScript's text item delimiters to prevTIDs
    return textString
end replaceText

on writeTextToFile(theText, theFile, overwriteExistingContent)
    try
        -- Convert the file to a string
        set theFile to theFile as string
        -- Open the file for writing
        set theOpenedFile to open for access file theFile with write permission
        -- Clear the file if content should be overwritten
        if overwriteExistingContent is true then set eof of theOpenedFile to 0
        -- Write the new content to the file
        write theText to theOpenedFile starting at eof
        -- Close the file
        close access theOpenedFile
        -- Return a boolean indicating that writing was successful
        return true
        -- Handle a write error
    on error
        -- Close the file
        try
            close access file theFile
        end try
        -- Return a boolean indicating that writing failed
        return false
    end try
end writeTextToFile

Upvotes: 1

Related Questions