codingguy3000
codingguy3000

Reputation: 2835

AppleScript choose file dialog box store last folder opened in text file.

This seems like such an easy thing to do that I'm amazed the solution is still eluding me.

I'm trying to store that last folder opened by a "choose file" dialog box. I want to store that folder's location in a text file.

I want to have my "choose file" dialog always open to the last folder used.

I've got a lot of the script working but there is one weird thing that keeps eluding me.

Look at my script...

set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")

set strPathFromConfig to item 1 of Shows as string

set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)

display dialog strPathFromConfig

set strPath to (path to home folder as text) & strPathFromConfig

display dialog strPath

choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath

The script reads my config text file which contains one line and only one string of "Documents".

I trim some leading garbage characters and display a dialog with the result of "Documents".

Then I set the strPath using the the value from the config file.

I display the new value is it is a valid location on my system.

Next I attempt to the dialog and I get an error message of "File alias Macintosh HD:Users:lowken:Documents of wasn't found.

Let change the script so that instead of using the value that was extracted from the config.txt file, I simply set a string variable in my script.

set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")

set strPathFromConfig to item 1 of Shows as string

set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)

display dialog strPathFromConfig

set strTemp to "Documents"

set strPath to (path to home folder as text) & strTemp

display dialog strPath

choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath

Now it works. AppleScript seems to not want to use the value that was looked up from the config.txt file.

What am I doing wrong? I tried casting to an Alias I tried different locations on my system.

It seems to me that somehow the value looked up from the text file is not a string data type.

Any ideas?

P.S.

I'm running OS X Yosemite 10.10.4 on a mid 2012 MacBook Pro.

Upvotes: 0

Views: 656

Answers (2)

jackjr300
jackjr300

Reputation: 7191

The problem is the encoding of your "config.txt" document.

The read command can read (MacRoman, UTF-8 or UTF-16) encoded text files, but you must specify the type class, otherwise it read the text file as MacRoman.

set strPathFromConfig to paragraph 1 of (read "/Users/lowken/Documents/config.txt" as «class ut16») -- or if UTF-8 use —> as «class utf8»
set strPath to ((path to home folder as text) & strPathFromConfig) as alias
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location strPath

For another encoding, you must change the encoding of the "config.txt" document.

Upvotes: 1

pbell
pbell

Reputation: 3105

The default location of the choose file command can be a Unix path "/Users/my_user/Documents" or a Finder path with Alias before like : alias "HD:Users:my_User:Documents" so first check that strPath is the correct value, then if OK, double check the class of it :

Display Dialog (class of strPath) as string

it may not be OK and you have to coerce the (path to home folder as string) and not text.

Upvotes: 1

Related Questions