OK, I have an action button in my app, which lets the user choose a folder to process. Upon clicking the “OK” or “Choose” button, the action starts.
Now I’d like it to be more practical for the users : if they choose the wrong folder and click “Enter” on their keyboard by error, the app does its job and doesnt ask any questions …
To remedy to that, I’d like to use a pop-up NSpathcontrol object for them to choose a folder BEFORE they launch the action.
So my question is, how can I get the path chosen in the pathcontrol object with my action button and make sure that only the chosen folder gets processed ?
(the documentation doesnt help, I feel like it’s really aimed at experts and the more I look at it, the more confused I am. )
NB : I was thinking I could also just add a button whose only action would be to choose the folder to process, but I’d have to make sure the user sees the path to check if it’s correct (if that’s possible) – so I’d also have to display that path in a text field or something …
-- `string value` is the important part here.
get string value of control "pathControl" of whatever
-- Handle the file:// url that is returned by the path control
do shell script "/usr/bin/python -c '" & ¬
"from sys import argv; " & ¬
"from urllib import unquote; " & ¬
"from urlparse import urlparse; " & ¬
"print unquote(urlparse(argv[1])[2])' " & quoted form of result
set example to result
-- log example
OK, thanks for the quick answer, Bruce.
But I keep getting an error : “Finder got an error: Can’t get folder “/Applications/xxx/XxXxX/BlahBlah/”. (-1728)”
I have no clue how to fix this. The thing just doesnt behave the way I want it to.
I’m thinking I’d be better off using a regular button and set it to “choose file”, and then returning the chosen path as a string in a text field next to it. The problem I’ve run into before with this is how to encapsulate the “on clicked theObject” and "if (name of theObject is “XXX”) because I have 2 buttons in the same tab view item, which have to cooperate and work together (one to chooose the folder, the other to act on the chosen folder) – not sure this is clear!
Any idea how to do that ? anyway, i’m sure I’ll figure it out eventually.
Couldnt get your way to work … so I did a regular btton with a text field next to it to display the path chosen.
if (name of theObject is "chooseFile") then
chooseDefaultLocation()
set contents of text field "path name" of tab view item 1 of tab view 1 of window "main" to path name of open panel
end if
on chooseDefaultLocation()
-- Setup the open panel properties
tell open panel
set can choose directories to true
set can choose files to false
end tell
display open panel attached to window "main"
end chooseDefaultLocation
on panel ended theObject with result withResult
end panel ended
It does work, the path appears as text in the text field with slashes “/” to separate the folders and all, that’s great (although when I change the path, I need to open the panel twice for it to display the full new path – otherwise, the path chosen before remains in the text field).
However, upon clicking my action button, I get an error :
I’m sure I need to convert the text displayed into an actual path recognized by my action button as the place where to look, but could anyone help me do that please ? I cant seem to find a way to do that (i used POSIX blah blah, but I’m not too sure how that works yet…).
There’s a good chance that my app will work after that (we’ll see!) so I can’t wait
Thanks
My point was that the scripts that you and I (and Stefan) posted end up with a POSIX path.
Should it? I can’t see exactly what you are doing with that value, so I can’t definitively say.
Try using POSIX file.
choose folder
set example to result
display dialog (example as Unicode text)
set example to POSIX path of example
display dialog example
set example to POSIX file example
display dialog (example as Unicode text)
This is all really confusing to me as it’s the first time I’m using POSIX file, and paths and all that.
What I’m trying to do is basic: I want to get the path of the chosen folder (from the text field that the path is displayed in), so that my action button acts in that path.
But I still get the same error …
Here is my code:
on clicked theObject
if (name of theObject is "chooseFile") then
chooseDefaultLocation()
set folderChosen to result
set contents of text field "path name" of tab view item 1 of tab view 1 of window "main" to path name of open panel
end if
if (name of theObject is "actionButton") then
set source_folder to POSIX file folderChosen
-- my actions and code
end clicked
on chooseDefaultLocation()
-- Setup the open panel properties
tell open panel
set can choose directories to true
set can choose files to false
end tell
display open panel attached to window "main"
end chooseDefaultLocation
The path is displayed fine, like I said, but then when I click the action button it says it get cant get folder “/applications/blahblah/…”
What am I missing here ?
If you attach the panel to a window, you should use the panel ended handler to process the result
property folderChosen : ""
on clicked theObject
if (name of theObject is "chooseFile") then
chooseDefaultLocation()
else if (name of theObject is "actionButton") then
set source_folder to POSIX file folderChosen as text -- HFS path e.g.("Mac HD:Applications:.")
-- my actions and code
end if
end clicked
on panel ended theObject with result withResult
if withResult is 1 then
set folderChosen to path name of open panel -- returns POSIX path e.g. ("/Applications/.")
set contents of text field "path name" of tab view item 1 of tab view 1 of window "main" to folderChosen
end if
end panel ended
on chooseDefaultLocation()
-- Setup the open panel properties
tell open panel
set can choose directories to true
set can choose files to false
end tell
display open panel attached to window "main"
end chooseDefaultLocation
My bad, stupid me figured out the property line after I posted that !
But I still get an error :
Also, if I do use
-- ...
else if (name of theObject is "actionButton") then
set source_folder to POSIX file folderChosen as text -- HFS path e.g.("Mac HD:Applications:.")
-- my actions and code
on panel ended theObject with result withResult
if withResult is 1 then
set folderChosen to path name of open panel -- returns POSIX path e.g. ("/Applications/.")
end if
end panel ended
… the text field isnt set to the path. I have to put it this way for the path to be displayed in the txt field :
if (name of theObject is "chooseFile") then
chooseDefaultLocation()
set contents of text field "path name" of tab view item 1 of tab view 1 of window "main" to path name of open panel
else
--...
I’ve tried looking at the applescript studio examples located in /Developer/Examples (in the archive maker example and the display panel example), but I dont see anything wrong with my code to be honest.
I first thought that’s what it was … and I made sure of that
But you put me on the right path, because I never pay enough attention, and I wasnt using one of my MANY, MANY conditions right – so now it IS displayed in the text field, yay !!
But I still do get that error when i click the action button !!
I’m sure it’s because of that line :
set source_folder to POSIX file folderChosen as text
NB : my application processes the entire content of a folder, not a specific file. I want to only choose a folder, and for its whole content to be processed if you know what I mean.
Here it seems to be looking for a file in particular … no ?
The cause is in the code, you haven’t posted.
You choose a folder but the error message displays a file path.
I guess, while concatenating the path a colon is missing after myfolder
I can’t imagine posting the entire code, theres too much, and I dont really know what part of my code could potentially take out that colon at the end … ( You want the code by PM ? )
Thanks so much for your help Stefan, I really appreciate it.
In all honesty, I don’t know what I would do without this community