Page 1 of 1

extracting the path only of a selected file.

Posted: Wed Oct 02, 2013 2:59 am
by Bruce Brown
I am writing a very simple program that allows a user to select a file then in a number of copies field
it will copy the file to the same directory as default but will also allow the user to select a different directory if they wish.
The Gui is simple:
Select file
Display filename only in field of file selected. In directory field put the path of file originally selected.
In the number of copies field which has default of 1
Do it button that copies the file and appends the copy count to original filename so.
Here is the code for my browse button for selecting the file. I don't know how to select the path before the filename.
The part in < > is the command necessary to select all items of path except the last item.
I should know this by now but have not had anytime to play (learn).

on mouseUp
## Ask the user to choose a file
answer file "Please choose a file"
## If the dialog is not cancelled put the path to the selected file into a variable
if the result is not "cancel" then
put it into tFilename
set itemDelimiter to slash
put last item of tFilename into field "file2Dupe"
put <path of original file> tFilename into field "outputDirectory"
end if
end mouseUp

Re: extracting the path only of a selected file.

Posted: Wed Oct 02, 2013 5:21 am
by dunbarx
I am not sure I understand what you want, but could it be that you wish to:

put item 1 to -1 of it into fld "outputDirectory"

?

If this does it, can you think of another way to do this using the "number of items of it"?

Craig Newman

Re: extracting the path only of a selected file.

Posted: Sat Oct 05, 2013 4:56 pm
by Bruce Brown
Hi I did not get back to this until today but you jogged my memory of the syntax.
Here is the correct code that works!
on mouseUp
## Ask the user to choose a file
answer file "Please choose a file"
## If the dialog is not cancelled put the path to the selected file into a variable
if the result is not "cancel" then
set itemDelimiter to slash
put number of items of it into pathLength
put last item of it into field "file2Dupe"
put item 1 to pathLength-1 of it into field "outputDirectory"
end if
end mouseUp

Re: extracting the path only of a selected file.

Posted: Sat Oct 05, 2013 5:06 pm
by Klaus
Hui Bruce,

you can have it shorter :D

Code: Select all

...
if the result is not "cancel" then
  set itemDelimiter to "/"
  put item -1 of it into field "file2Dupe"
  put item 1 to -2 of it into field "outputDirectory"
end if
...
Best

Klaus

Re: extracting the path only of a selected file.

Posted: Thu Oct 10, 2013 1:32 am
by Bruce Brown
Hi Klaus,
I forgot to say thank you. You nailed it. My program is complete and works great. Small insignificant it is but got to start somewhere!