I have a very simple script, that when I drop a file on it, takes the file, makes a copy, and saves a copy in a new name. I can not seem to extract out the path of the parent folder of the dropped file.
For now, I store it on the Desktop as a test to make sure it works. I was thinking, I could take the path to the file, and split it up on the “:” and make a new path, but that seems very inefficient, any suggestions?
on open theFiles
set theCount to number of items in theFiles
repeat with i from 1 to theCount
tell application “Finder”
set dropped_file to (item i of theFiles as text)
set TheName to name of (dropped_file as alias)
set dropped_file to alias dropped_file as text – maybe do not need this
set theImageFile2 to alias “earth:Users:haneda:Desktop:”
set dupe_action to duplicate file dropped_file to theImageFile2
set name of dupe_action to (“test” as Unicode text)
end tell
end repeat
end open
*There is a lot more to this script, I just distilled it down to a simple test case for the sake of posting it here. Thanks to anyone who can help.
Try this subroutine to extract the parent folder path of an item:
set mypath to (path to me as Unicode text)
set parentfolderpath to my getparentfolderpath(mypath)
on getparentfolderpath(itempath)
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set itemcount to (count text items of itempath)
set lastitem to the last text item of itempath
if lastitem = "" then
set itemcount to itemcount - 2 -- folder path
else
set itemcount to itemcount - 1 -- file path
end if
set parentfolderpath to text 1 thru text item itemcount of itempath & ":"
set AppleScript's text item delimiters to olddelims
return parentfolderpath
end getparentfolderpath
The routine was written by Peter Fischer and I use it all the time.
try this, the Finder and System Events have a property container, which contains the parent folder of the item
on open theFiles
repeat with oneFile in theFiles
tell application "Finder"
set destinationFolder to container of oneFile
set dupe_action to duplicate oneFile to destinationFolder
set name of dupe_action to "test"
end tell
end repeat
end open