codingguy3000
codingguy3000

Reputation: 2835

AppleScript how to write a file path to a text file

I'm at near the end of building a new feature for my AppleScript.

I'm looking to be able to prompt the user to select an Excel file and then process that Excel File.

The new feature is that I want to store the file path of the file the user last selected so that the next time the script is executed the dialog box opens to the same folder.

I've got my dialog box working and I also have the file write piece working.

My issue is that I want to be able to write the file path to the text file and I don't know how.

Consider the following code:

set theFile to choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"}
display dialog (theFile as string)

set outputFile to (("Macintosh HD:Users:lowken:Documents:") & "LaunchAgent_Alert.txt") 

try
    set fileReference to open for access file outputFile with write permission
    write theFile to fileReference
    close access fileReference
on error
    try
        close access file outputFile
    end try
end try

The code works, however I'm getting garbage in the output file:

>Macintosh HDÀ·q†H+÷œMiamieMasterMind.xlsxó∑èœÇäRXLSXXCELˇˇˇˇI À·©‡œÇ¬í,MiamieMasterMind.xlsxMacintosh HD*Users/lowken/Dropbox/MiamieMasterMind.xlsx/
ˇˇ

My guess is that either I have a file encoding issue or I need to output the file path from theFile.

Your help is appreciated.

Upvotes: 1

Views: 2205

Answers (3)

jackjr300
jackjr300

Reputation: 7191

You can save an appleScript’s class and read it as (the type class).

Examples

  1. write theFile to fileReference — theFile is an appleScript’s alias

    read it like this —> set theFile to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as alias

  2. If you save a list:

    write myList to fileReference — myList is an appleScript’s list

    read it like this —> set myList to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as list

  3. If you save a record —> {b: "15", c:"éèà"} :

    write myRecord to fileReference — myRecord is an appleScript’s record

    read it like this —> set myRecord to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as record

  4. If you save a real —> 200.123 :

    write floatNumber to fileReference — floatNumber is an appleScript’s number

    read it like this —> set floatNumber to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as real

  5. If you save an integer —> 20099 :

    write xNum to fileReference — xNum is an appleScript’s integer

    read it like this —> set xNum to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as integer

  6. If you save a string —> "éèà:376rrrr" :

    write t to fileReference — t is an appleScript’s string

    read it like this —> set t to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as string


Important : set eof to 0 before writing a new contents to an existing file

set fileReference to open for access file outputFile with write permission
set eof fileReference to 0
write something to fileReference
close access fileReference

Upvotes: 1

pbell
pbell

Reputation: 3105

Usage of property explained by Craig is the easiest solution. The property values will be reset in case you recompile the script.

However, if you really need to store the path value in txt file for use by other scripts, you just need to write the file, not as alias, but as string :

write (theFile as string) to fileReference

Of course, when reading the text file later, remember it is a string and not an alias !

Upvotes: 2

Craig Smith
Craig Smith

Reputation: 1071

Try using a property, and the script will do all the work for you:

property theContainer : null
property theFile : null

set theFile to choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"}
tell application "Finder" to set theContainer to theFile's container

From the AppleScript Language Guide:

The value of a property persists after the script in which the property is defined has been run. Thus, the value of currentCount is 0 the first time this script is run, 1 the next time it is run, and so on. The property’s current value is saved with the script object and is not reset to 0 until the script is recompiled—that is, modified and then run again, saved, or checked for syntax.

Upvotes: 2

Related Questions