Apple Script not opening a CSV file in numbers first time

I have a CSV file that is being produced daily by a python application using pandas. I am running the python and apple script from within a shell script. The workflow works fine but it appears the first time numbers attempts to open the csv file I get the error "filename.csv can't be imported. The file couldn't be opened" If I open the file by double clicking on it in finder it opens ok then I close it (do not save) and re run the script it works perfectly (no changes to filename or location) I have tried both path types /.../filename.csv and alias Macintosh HD: ...:/filename.csv. I am also getting same with original Numbers and the new Numbers Creator Studio.


any advice would be appreciated


Thanks


Ron

Posted on Feb 4, 2026 3:33 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 4, 2026 1:11 PM

That's why seeing the script is important...


Let's work backwards from the bit that fails:


			set thisCSV to open filenameCSV


seemingly innocuous, until you look back and see that you...


set filenameCSV to "Macintosh HD:Users:roncarter:ha:unprocessed:" & fCSV


So filenameCSV is a text object. It is not a file. It's just a string of characters.


Numbers cannot open a text object.


Sure, to you and to me, that 'text object' sure looks like a path to a file, but Numbers (or AppleScript in general) won't make that leap of faith for you. You need to give it explicit direction (or, at least, a hint) that it's a file path.


Just change that line to:


		set thisCSV to open file filenameCSV 


and you should be good to go. You're coercing that text object to a file object, which Numbers is happy to open.


(note, coercing it to an alias is also an option, as you've noted).


4 replies
Question marked as Top-ranking reply

Feb 4, 2026 1:11 PM in response to rcarter45

That's why seeing the script is important...


Let's work backwards from the bit that fails:


			set thisCSV to open filenameCSV


seemingly innocuous, until you look back and see that you...


set filenameCSV to "Macintosh HD:Users:roncarter:ha:unprocessed:" & fCSV


So filenameCSV is a text object. It is not a file. It's just a string of characters.


Numbers cannot open a text object.


Sure, to you and to me, that 'text object' sure looks like a path to a file, but Numbers (or AppleScript in general) won't make that leap of faith for you. You need to give it explicit direction (or, at least, a hint) that it's a file path.


Just change that line to:


		set thisCSV to open file filenameCSV 


and you should be good to go. You're coercing that text object to a file object, which Numbers is happy to open.


(note, coercing it to an alias is also an option, as you've noted).


Feb 4, 2026 10:18 AM in response to rcarter45

You kind of need to show your script to be able to debug this kind of issue.


Paths can be tricky in AppleScript due to multiple, slightly different definitions of a file, an alias, a POSIX file, etc., each with their own nuances (and 'alias Macintosh HD:../filename.csv' would never be correct since aliases use Mac-style(colon-delimited) paths, so that / would be a problem). But since it works on the second run I'll assume that's just a transcription issue.


Second thought would be a race condition. Is the file completely quiesced and closed by Python before you try to open in it Numbers?

Feb 4, 2026 11:59 AM in response to Camelot

Camelot


Thanks for your response. I can get the same situation if I just have part of the script without the python part so no race condition.


If I run the script a newly created csv I always get the can't be imported error I can run the script over and over on the same file and same result.


if I open the file in numbers either using finder or by selecting it from file open in numbers the file opens and is imported. Sometimes I can close the file and then run the script and it works fine, If I leave the csv opened and then run the script it will open another instance of the csv file and will always work correctly.


To me it looks like there is something different between the csv importer when run from within numbers and when started from Apple Script. If I knew what the difference was I may be able to modify the csv file but I have no idea where to start. The cut down shell script and the file opening part of Apple script are below


thanks for your assistance


Ron


#!/usr/bin/env bash




DF="out-2026-02.csv"


echo "__________________________________"


set -euo pipefail


osascript /Users/roncarter/development/ha/scripts/updateenergyusages.scpt $DF


echo "=================================="



Apple Script


on run args

set fCSV to item 1 of args

log fCSV

set filenameCSV to "Macintosh HD:Users:roncarter:ha:unprocessed:" & fCSV

log filenameCSV


tell application "Numbers Creator Studio"

activate


try

set thisCSV to open filenameCSV

on error errorMessage number errorNumber


if errorNumber is not -128 then

display alert errorNumber message errorMessage

end if


end try




Apple Script not opening a CSV file in numbers first time

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.