Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can't find a renamed file using Applescript

Here is what I'm trying to accomplish:

1) User selects .csv file

2) Finder duplicates file and renames as a txt file

3) Microsoft Excel imports this newly made file as a pipe delimited text file with text formatting in all cells (On a Mac, Excel won't take pipe delimited csv's, it automatically assumes they are comma delimited)

4) Microsoft Excel then saves this as the same name of the original user extraction location with the same name but in the .xlsx or XML format

Preferably with that duplicated file now gone, whether its used and converted with Excel, or later deleted from Finder.


Here's the code that I'm working with so far:


on run

set csvfile to (choose file) as alias


tell application "Finder" to set tempname to (name of csvfile as text) as text

--display dialog "" & tempname


tell application "Finder"

set theDupe to duplicatecsvfile

set name extension of theDupe to "txt"

end tell


tell application "Microsoft Excel"


open text filefilenametheDupedata typedelimitedfield info {{1, text format}, {2, text format}, {3, text format}, {4, text format}, {5, text format}, {6, text format}, {7, text format}, {8, text format}, {9, text format}, {10, text format}, {11, text format}, {12, text format}, {13, text format}, {14, text format}, {15, text format}, {16, text format}, {17, text format}, {18, text format}, {19, text format}} other char "|" with use other

set theworksheet to worksheet 1 of active workbook


save astheworksheetfilenametempnamefile formatExcel XML file format with overwrite

end tell

end run



The error I run into is that after it duplicates and renames the file, it is trying to refer to something that it can't find.


Please I'm stuck at this point, any suggestions or helpful hints?? 🙂



MacBook Pro (Retina, 15-inch, Early 2013), OS X Mavericks (10.9.2)

Posted on Apr 3, 2014 7:33 PM

Reply
Question marked as Best reply

Posted on Apr 3, 2014 9:32 PM

My Applescript is rusty but it seems to work if you pass 'theDupe' filename as a string…



open text filefilenametheDupe as stringdata typedelimitedfield info {{1, text format}, {2, text format}, {3, text format}, {4, text format}, {5, text format}, {6, text format}, {7, text format}, {8, text format}, {9, text format}, {10, text format}, {11, text format}, {12, text format}, {13, text format}, {14, text format}, {15, text format}, {16, text format}, {17, text format}, {18, text format}, {19, text format}} other char "|" with use other

I'm not sure that is correct but it seemed to do something with my test.txt file.…weird.

NOTE: this is on 10.6.8 With Excel 12 !

13 replies
Question marked as Best reply

Apr 3, 2014 9:32 PM in response to fermanjj

My Applescript is rusty but it seems to work if you pass 'theDupe' filename as a string…



open text filefilenametheDupe as stringdata typedelimitedfield info {{1, text format}, {2, text format}, {3, text format}, {4, text format}, {5, text format}, {6, text format}, {7, text format}, {8, text format}, {9, text format}, {10, text format}, {11, text format}, {12, text format}, {13, text format}, {14, text format}, {15, text format}, {16, text format}, {17, text format}, {18, text format}, {19, text format}} other char "|" with use other

I'm not sure that is correct but it seemed to do something with my test.txt file.…weird.

NOTE: this is on 10.6.8 With Excel 12 !

Apr 4, 2014 6:38 AM in response to fermanjj

You need to state that csvfile is a file and reference as alias

(You can see this with display dialogtheDupe as string)


tell application "Finder"

set theDupe to (duplicatefilecsvfile) as alias

set name extension of theDupe to "txt"

end tell

--display dialog theDupe as string


And as Drew said, in Excel, open as type string:

open text filefilename (theDupe as string)

Apr 4, 2014 10:04 AM in response to fermanjj

fermanjj wrote:


This did the trick! Would it be possible to save the worksheet as an xlsx with the same name in the folder that it was grabbed from?



Is this what you mean:


set csvfile to (choose file) as alias

tellapplication "Finder" tosetparentFoldertocontaineroffilecsvfile

tell application "Finder" to set tempname to (parentFolder as string) & (name of csvfile as text)

Apr 4, 2014 10:24 AM in response to Tony T1

Yes this does help, its almost there, but one thing is it keeps saving it with the extension as .csv in the xlsx format somewhere, I don't know where. So I have to save-as in the parent folder again. And it leaves the " copy.txt" file that was made from the Duplicate, in the parent folder as well. I thought it would be using this file and overwriting..?

Apr 4, 2014 11:40 AM in response to Tony T1

This actually works, I just need to rename "theDupe" file at the end to take off the " copy"


on run

set csvfile to (choose file) as alias


--tell application "Finder" to set parentFolder to container of file csvfile

--tell application "Finder" to set tempname to (parentFolder as string) & (name of csvfile as text)

--display dialog "" & tempname

tell application "Finder"

set theDupe to (duplicate file csvfile) as alias

set name extension of theDupe to "txt"


--display dialog theDupe as string

end tell


tell application "Microsoft Excel"


open text filefilename (theDupe as string) data typedelimitedfield info {{1, text format}, {2, text format}, {3, text format}, {4, text format}, {5, text format}, {6, text format}, {7, text format}, {8, text format}, {9, text format}, {10, text format}, {11, text format}, {12, text format}, {13, text format}, {14, text format}, {15, text format}, {16, text format}, {17, text format}, {18, text format}, {19, text format}} other char "|" with use other

set theworksheet to worksheet 1 of active workbook


save astheworksheetfilename (theDupe as string) file formatExcel XML file format

end tell

--tell application "Finder" to delete file (theDupe as string)

tell application "Finder" to set name extension of theDupe to "xlsx"

end run

Apr 4, 2014 11:51 AM in response to fermanjj

Just create the filename you want to save as at the start of the script.


set csvfile to (choose file) as alias

tell application "Finder"

set parentFolder to container of filecsvfile

set tempname to (parentFolder as string) & (name of csvfile as text)

set excelName to text 1 thru ((offset of "." in tempname) - 1) of tempname & ".xlsx"

end tell


display dialogtempname

display dialogexcelName



Then replace tempname with excelName when you save the excel file

Can't find a renamed file using Applescript

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