pdfapplescriptexporttextedit

Applescript open file with TextEdit and export them as pdf


This is my very first applescript. I use automator to launch the script as I drop new files to a folder (Folder action).

In Automator I have 2 action: 1- Get Specified Finder Items 2- The Apple Script

But I can't make it running. The script stops without warning after opening the files.

Here is the script:

on run {input, parameters}

tell application "TextEdit" to activate

repeat with theFile in input

    set theFilePath to theFile as alias

    tell application "TextEdit" to open theFilePath

    tell application "System Events"
        tell process "TextEdit"
            set foremost to true
            click menu item "Export as PDF..." of menu "File" of menu bar 1
            click button "Save"
            click menu item "Close" of menu "File" of menu bar 1
        end tell
    end tell

end repeat

return input
end run

Can anyone help me on this?

I just want to export all the files in the specified folders to pdf using TextEdit.

Thanks


Solution

  • on adding folder items to this_folder after receiving these_items
        repeat with i from 1 to number of items in these_items
            set this_item to item i of these_items
            tell application "TextEdit"
                activate
                open this_item
                delay 1
                tell application "System Events"
                    click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1 of application process "TextEdit"
                    delay 1
                    key code 36
                    delay 1
                    key code 13 using command down
                end tell
            end tell
        end repeat
    end adding folder items to
    

    This alternate version will quit TextEdit application after converting the files to .PDF. Be careful using this version because I set it to quit without saving any open documents if TextEdit was already running.

    on adding folder items to this_folder after receiving these_items
        repeat with i from 1 to number of items in these_items
            set this_item to item i of these_items
            tell application "TextEdit"
                activate
                open this_item
                delay 1
                tell application "System Events"
                    click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1 of application process "TextEdit"
                    delay 1
                    key code 36
                    delay 1
                    key code 13 using command down
                end tell
            end tell
        end repeat
        ignoring application responses
            tell application "TextEdit" to quit without saving
        end ignoring
    end adding folder items to
    

    If you save this following AppleScript in script editor as “Convert_To_PDF.scpt” in your /Users/INSERT YOUR USERNAME/Library/Workflows/Applications/Folder Actions folder. You don't need to use Automator at all. You simply need to control + click any folder you want as your “Hot Folder” and you will see this:

    enter image description here

    After you select “Folder Actions Setup”, you will see this:

    enter image description here

    Because you saved that AppleScript in your /Library/Workflows/Applications/Folder Actions folder, when you click the + sign to add a script to your folder for the folder action, it will automatically be available in the list of scripts to choose from

    enter image description here

    enter image description here

    You may need to adjust the delay settings in the AppleScript code a little bit. Either way, this functions for me in the latest version of Sierra.