0

To preface: My Applescript takes an Excel file, and copies its data into a new plain text file.

I am having trouble writing the new text file in the same location as my existing Excel file, but without the filename of the original file.

For example: My Excel file is "FilenameA.xls" and when I save my new text file, it saves it as "FilenameA.xlsFilenameB.txt"

How can I save this new file to the same location, but with my own filename?

NOTE: It works when I use (path to desktop as text) but I won't always be saving these to my desktop.

set myFile to open for access (path to desktop as text) & blahBlah & ".txt" with write permission

When I try the script below, it gives me the original filename PLUS my new file name.

set myFile to open for access (fileName) & blahBlah & ".txt" with write permission

NOTE: fileName refers to the path of the original Excel file.

Essentially I want the same results as (path to desktop as text) but with the flexibility of saving the file to whatever folder in which the original was accessed.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
pianoman
  • 815
  • 2
  • 16
  • 36

2 Answers2

1

Thank you for clarifying your request. Basically, you needed to add a "container of" line in your code, and use that in the new file path name. If you posted more of you code, I could adapt its syntax in place, but this should be enough for you to figure it out. Here's some sample code that allows you to select an excel file, collects the container of that file, prompts you for a new filename, and then allows you to write the new txt file:

set excelFile to (choose file with prompt "Choose Excel file :" of type {"xls", "xlsx"} without invisibles)

tell application "Finder"
    set containerPath to (container of excelFile) as string
    set oldFilename to name of excelFile
end tell

set newFilename to text returned of (display dialog "Enter name of the resulting text file" default answer oldFilename)

set myFile to open for access (containerPath & newFilename & ".txt") with write permission
try
    -- add content to myFile
    write "Here's some groovy content added to the file." to myFile
    close access myFile
on error
    close access myFile
end try
jweaks
  • 3,674
  • 1
  • 19
  • 32
  • My issue is that I'm using the path from the original file to save the new file. When I use the path with `open for access`, it places the original filename in front of my custom parameters. I would like for my new file to bear the filename I choose. – pianoman Feb 26 '14 at 14:38
  • I edited the question to better reflect my intent. Does that help? – pianoman Feb 26 '14 at 14:42
  • Yes, helpful clarification. This sample code in the revised answer should solve things for you. – jweaks Feb 26 '14 at 15:35
0

I have a couple of suggestions for improvement to the above solution.

If you use the "displayed name" of the Excel file, it won’t include the file extension:

set oldFilename to displayed name of excelFile

That way, you will end up with “Spreadsheet.xlsx” and “Spreadsheet.txt” in the same folder, rather than “Spreadsheet.xlsx.txt.”

And you don’t have to write your own text file like that with open for access, because TextEdit can write text files for you:

tell application "TextEdit"
    make new document with properties {text:"The text file contents."}
    close document 1 saving in file (containerPath & newFilename & ".txt")
end tell

That way you are utilizing TextEdit’s sophistication with text files. You create a UTF-8 text file and there is no risk of leaving a file open for write access or any other low-level disk errors.

You can also use TextEdit to open an existing file and add text to the end of it and save again:

tell application "TextEdit"
    open file (containerPath & newFilename & ".txt")
    set the text of document 1 to the text of document 1 & return & "Some more text."
    close document 1 saving yes
end tell
Simon White
  • 686
  • 5
  • 9