0

I have a massive spreadsheet with a titanic number of rows/columns (e.g. ~250 columns, many thousands of rows) that I'm trying to convert into PDFs by looping through each row with AppleScript, copying that row's ~250 variables to TextEdit set to Rich Text (for bold formatting etc), and then using System Events to save the txt as a PDF. Here's a summary of the code:

on run
    set initialRow to 1
    tell application "System Events"
        tell application process "TextEdit"
            set frontmost to true
        end tell
    end tell
    repeat
        tell application "Microsoft Excel"
            -- CLEAR MY ~250 VARIABLES FROM PREVIOUS ROW'S VALUES TO MAKE SURE NOTHING IS CARRIED OVER BY MISTAKE
            -- THEN SET MY ~250 VARIABLES TO THE NEXT ROW'S VALUES
            if exampleValue is "" then exit repeat
        end tell
        tell application "TextEdit"
            set the text of the front document to ""
            -- THEN SET FIRST PARAGRAPH TO MY FIRST VARIABLE PLUS A LINE BREAK SO THEN THERE'S A NEW PARAGRAPH FOR THE NEXT VARIABLE, ETC
            -- THEN GO THROUGH ALL OF MY VARIABLES TO IMPORT THE IMPORTANT ONES INTO TEXTEDIT, SET SOME FORMATTING, ETC.
        end tell
        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
            keystroke exampleValue -- SYSTEM EVENTS TYPES THE NAME OF THE PDF
            delay 1
            key code 36
            delay 1
        end tell
        set myRow to (myRow + 1)
    end repeat
end run

This all runs great, no bugs (seemingly!), no issues at all in small doses. The problem, however, is that something happens as the script runs where it seems to be tying up more and more memory somewhere; everything is fine for the first hundred or so rows, but at some point my Mac stops running anything at all, i.e. whether I let the script run until it starts producing super random errors (I could collect them if helpful, but it's like a random different error each time so not much help there) or even if I let the script run for a while and then stop it before it errors out - it will let me stop the script but then I can't actually quit out of Script Editor or TextEdit or Excel, my keyboard stops working, I can't Force Quit anything, can't Reset the computer, etc. It's just a complete meltdown of the machine unlike anything I've encountered, and the only way to get back to work is to force a hard boot with the power button.

I've never had this problem with my other scripts, but I also don't usually use System Events, so my hunch is that it's something to do with that. Do I need to be 'resetting' System Events somehow, or clearing out the memory for some reason, or...? Thanks for the help!!

Damien X
  • 41
  • 3
  • I think I'd start by exporting the whole Excel document to csv and to work on that instead. I can't tell you what's causing your specific issues - or what those rogue processes are - but AppleScript is IME notorious for memory leakage across long iterations. – Lorccan Jan 28 '22 at 10:47
  • Just curious, is there a reason why CSV would be preferred? I'd still be opening it in Excel, or you mean run CSV in a browser or XCode or something? – Damien X Jan 28 '22 at 16:03
  • Not especially - but any text format with predictable field separators is going to be easier to manipulate than using GUI scripting in combination with Microsoft’s implementation of AppleScript in Excel. – Lorccan Jan 28 '22 at 16:06

1 Answers1

1

Figured it out! After trying the script one more time with Activity Monitor running, I discovered that each time it iterates through, 3 new processes were popping up - Core Sync, Dropbox Finder Extension, and SnailSVNLite - and then never going away! So if I ran through the script 500 times, I'd end up with 1500 new processes running, which was almost certainly what was wrecking me though I have no idea why telling System Events anything was doing that. I looked around online, and it turns out those are all Finder Extensions that had been turned on at some point long ago, so just needed to go to System Preferences > Extensions > Added Extensions and then uncheck those 3 extensions - and then problem solved!!

Damien X
  • 41
  • 3