I’m trying to get the path to a file and am having problems. I’ve tried several syntax variations and nothing is working for me. On reading the dictionary for Standard Additions, it says that “path to” is valid for folders and apps. How do I get the path of ANY file?
first I get a list of the items on the disk:
tell application "Finder"
set aDisk to choose folder with prompt "Select the disk to add to database."
set myList to (every item of aDisk)
set aFile to choose file with prompt "Where is the Appleworks disk file?"
end tell
Then I process the list in a handler:
repeat with myItem in theList
if kind of myItem is "folder" then
--announce processing of the folder
try
say "Processing folder " & (displayed name of myItem)
end try
--tunnel down to next folder level
set newList to (every item of myItem)
my addData(newList, theDisk, theFile)
else
--Get the file info
tell application "Finder"
copy displayed name of myItem to myfile
copy physical size of myItem to mySize
copy displayed name of disk theDisk to myDisk
--The next line tries to get the path, but doesn't work
copy (path to (container of myItem)) to myPath
copy modification date of myItem to modDate
copy (current date) to catDate
copy description of myItem to myType
end tell
...snipped to end of IF and REPEAT statements
The script editor keeps telling me that it can’t make (something, depends on the syntax I’ve tried) into an item. If it’s an item in a list of items, why can’t it be made into an item?
Model: iMac DV+
AppleScript: 1.9.1
Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.1) Gecko/20060214 Camino/1.0
Operating System: Mac OS X (10.2.x)
well, I tried “path to myItem” and it doesn’t work in this context for some reason. I’m looking for the string path along the lines of “Macintosh HD:Users:Nitewing:Documents:My File.txt” so that I can enter that information into an appleworks database field.
When I try to use the ((path to (container of myItem)) syntax, the error message reads: Finder got an error: Can’t make container of document file “Apple Desktop Pack.sit” of disk “Wilma” into a item.
I tried using the “container” syntax because when I tried “path to myItem”, I got this error: Finder got an error: Can’t make document file “Apple Desktop Pack.sit” of disk “Wilma” into a item.
Ok, I was trying to not clutter things with lots of code, but I see that the context I’m using is relevant, so here is the whole script:
(* main script *)
tell application "Finder"
set aDisk to choose folder with prompt "Select the disk to add to database."
set myList to (every item of aDisk)
set aFile to choose file with prompt "Where is the Appleworks disk file?"
end tell
tell application "AppleWorks 6"
activate
end tell
my addData(myList, aDisk, aFile)
try
say "Done Processing disk " & displayed name of aDisk
end try
display dialog "Done Processing disk " & displayed name of aDisk
(* handlers *)
on addData(theList, theDisk, theFile)
-- recursive handler to tunnel into folders
repeat with myItem in theList
if kind of myItem is "folder" then
--announce processing of the folder
try
say "Processing folder " & (displayed name of myItem)
end try
--tunnel down to next folder level
set newList to (every item of myItem)
my addData(newList, theDisk, theFile)
else
--Get the file info
tell application "Finder"
copy displayed name of myItem to myfile
copy physical size of myItem to mySize
copy displayed name of disk theDisk to myDisk
copy (path to (folder of myItem)) as string to myPath
copy modification date of myItem to modDate
copy (current date) to catDate
copy description of myItem to myType
end tell
--Open the AW db and begin data transfer
tell application "AppleWorks 6"
open theFile
copy the name of the front document to docName
-- transfer the record
make new record at end of document docName
set value of field "Name" of last record of document docName to myfile
set value of field "Size" of last record of document docName to mySize
set value of field "Disk" of last record of document docName to myDisk
set value of field "Path" of last record of document docName to myPath
set value of field "Mod Date" of last record of document docName to modDate
set value of field "Cat Date" of last record of document docName to catDate
set value of field "Type" of last record of document docName to myType
end tell
end if
end repeat
end addData
The script worked fine until I added the “path to” line, now it give me errors because it says (idiotically enough) that it cannot make “document blah of folder foo into an item”. By definition, aren’t ALL folders and files items?
I think part of your problem is how you’re defining your problem.
You already have a path. What you want is the directory part of it (or the path’s parent object, etc., etc. You can call it quite a few things, but you shouldn’t call it “path”).
Ignore the error message, I think “path to” is just confused.
Take a look at the “path to” commands in StandardAdditions’ dictionary. the keywords listed there are the only things that the “path to” commands will accept.
The thought is very much appreciated, but in this case it may be helpful to have more.
You’re welcome! I’ll leave you with something else to think about.
tell application "Finder"
activate
set theDisk to choose folder with prompt "Select the disk to add to database."
set theDiskName to displayed name of result
set awFile to choose file with prompt "Where is the Appleworks file?" without invisibles
set fileList to {displayed name, physical size, it, modification date, description} of (files of entire contents of theDisk)
end tell
tell application "AppleWorks 6"
activate
open awFile
set awName to name of front document
count (first item of fileList)
repeat with i from 1 to result
tell document awName
make new record at end
tell last record
set value of field "Name" to (item i of (item 1 of fileList))
set value of field "Size" to (item i of (item 2 of fileList))
set value of field "Disk" to theDiskName
set value of field "Path" to (item i of (item 3 of fileList)) as string
set value of field "Mod Date" to (item i of (item 4 of fileList)) as string
set value of field "Cat Date" to (current date) as string
set value of field "Type" to (item i of (item 5 of fileList))
end tell
end tell
end repeat
try
say "Done Processing disk " & theDiskName
end try
display dialog "Done Processing disk " & theDiskName
end tell
Note that Finder’s “entire contents” property can be quite slow on very large disks/folders.
I did like the “without invisibles” addition, I didn’t realize you could do that. I’m a bit curious about Finder’s file type property for items. All I get for file type for files is “msng” (which I think is tongue-in-cheek for ‘Microsoft’s no good’). Didn’t files used to get typed with a more descriptive four-letter code? I know OS X added “.” extensions to the end of file names, but are the old file types (TEXT, HTML, etc.) gone?
Apple still provides the mechanism for developers to use file types, but this is near to “left to developer’s decission”. Some will use them, someone won’t. And sometimes, it won’t be relevant if you use them or not, as the user can still define thru LauncServices who will open a given file-extension, disregarding the file type/creator.
That’s really too bad. One of the (IMHO) defining elements of the Mac from a programming standpoint was having that information available, especially from AppleScript. Heck, I made quite a living out of doing conversions for a newspaper that used both Mac and Winders machines. I much preferred working with the Mac files, as they gave me creator and file types so that I could decide which Winders type I should give it. Going the other way often left me wondering which program was best suited to the given file (in the context of what the user I was doing it for had available, program-wise).
It would be nice if Apple brought all this scattered info (some in old four letter types, some in file extensions, some in launch services) together so that a programmer could have an interface that would return something. There would have to be a publicly stated heirarchy as to which type would have precedence in given contexts, but it sure would be handy.
on open thisItem
set thePath to thisItem as string
end open
The thePath variable store the path of the dragged item.
You must save this script as application and drag the item (file or folder) over it.
If you drag more than one item, you can try:
on open theseItems
set thePaths to {}
repeat with i from 1 to (count of items of theseItems)
set end of thePaths to item i of theseItems
end repeat
end open
The thePaths list stores the paths of the dragged items
I don’t understand this. Applications like Tex-Edit Plus can make text files with extension “txt” open in it, but txt files are set to open with TextEdit. In a document based AppleScript Studio app, I can’t get a file with extension txt to open in the app with just a creator type for the app. How does Tex-Edit Plus do it?
When you say “are set to open with TextEdit”, what do you mean exactly?
By default, at least in my machine, .txt files without type/creator info are mapped by default to TextEdit. If you have a .txt with creator TBB6 (Tex-Edit Plus), they will open with TE+. If MSWD (or whatever is the creator for Word), they will open in Word. And so on…
This works so till I get-info for whatever .txt file, and I use the open-with disclosure button, choose TextEdit, then click the button change-all. From this point, any .txt file will be open by TextEdit, disregarding the creator info… This behaviour is recorded in the com.apple.LaunchServices.plist file as follow (OS 10.4.6):
To revert the behaviour, I think, you must delete this entry using something as Property List Editor, then logout-login or perhaps restart.
In your studio app, did you define “CFBundleDocumentTypes” for .txt extensions in your Info.plist file?