Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Choose files(s) or folder and process

How can I have a prompt to ask for file(s) or a folder?


I want to be able to use it to select a folder then process its contents as well as a manual selection of the files?


This what I use at the moment.

set these_items to (choose file with prompt "Choose file to record path name" with multiple selections allowed)


set weekNumber to ""

display dialog "Please enter the week number:" default answer weekNumber

set weekNumber to text returned of result

try

if weekNumber = "" then error

set weekNumber to weekNumber as number



end try


logthese_items

repeat with this_item in these_items


logthis_item

end repeat

iMac, OS X Mavericks (10.9.4)

Posted on Jun 3, 2015 2:03 AM

Reply
15 replies

Jun 3, 2015 3:36 AM in response to MattJayC

If you use Automator, you won't have to write your own NSOpenPanel to get files and folders. Here is the Ask for Finder Items with both files and folders enabled. Anything you select from this dialog will be passed to the next action as a list of aliases. In the Run AppleScript action, we collect that list from argv, and then branch processing based on the presence of a file or folder in that list. Included a comment on how to expand a folder into individual file list. This version does not recurse into sub-folders. You should be able to adapt your code to the following.


User uploaded file

Jun 3, 2015 8:35 AM in response to MattJayC

So here is something that I came up with for you. You use Automator to save a workflow that only contains the AskforFinderItems action set to files and folders, Desktop, and multiple selection. That is the only use of Automator needed. We will run that Automator workflow from within an AppleScript application that understands interactive (double-click), and drag & drop user functionality.


The sole purpose of the interactive part of the application is to use the above workflow to get user selected files and/or folders. No need for any other Automator actions, or the on run argv clauses.


The structure looks like the following:


property workflow : POSIX path of ((path to home folder) as text) & "AskforFinderItems.workflow"


on run-- Interactive section, only when user double-clicks


-- Automator will return a list of POSIX path objects with unwanted clutter

-- When AWK is done, only clean, quoted POSIX path filenames are returned.


set fileObjects to {}

set retObjs to paragraphs of (do shell script "/usr/bin/Automator " & workflow & " | awk '!/\\(|\\)/ {gsub(/\\x5c|,|\"/, \"\", $1);print $1;next}'")


-- roll our very own alias list

repeat with i from 1 to (count of retObjs)

copy (item i of retObjs as POSIX file as alias) & return to end of fileObjects

end repeat


-- log fileObjects


openfileObjects-- fileObjects is now open, so on open handler now takes over as we leave interactive

return

end run


on openfileObjects-- drag & drop section, or items passed from interactive section


-- one instance of your current run AppleScript code goes here. No need for Automator

-- at this point, or on run argv.


end open

Jun 3, 2015 8:45 AM in response to VikingOSX

Would it be fair to say that this means there would be 2 references to similar code? For example when I get it to run a process on each file, this bit of applescript will be in the automator action and the applescript application?


Can I get this in one wrapper?


propertyworkflow : POSIX pathof ((path tohome folder) astext) & "AskforFinderItems.workflow"

This line could be in the Resources of the applescript application.

in the applescript that processes the file I could also save the script (handler) to the resources folder.

But can the automator script reference an external script?

I hope that makes sense? I want to avoid duplicating the script so that if I make changes I only do it once.

Thanks for your help so far.

Matt

Jun 3, 2015 9:42 AM in response to MattJayC

The code structure that I provided replaces Automator, and is compiled as an AppleScript application. Only the Automator action, AskforFinderItems is saved as an Automator workflow, and is used by Automator in the do shell script portion of the on run interactive handler.


Once you have the files from the interactive section, your AppleScript code (only) that currently is in the Run AppleScript action — less the on run argv handler — goes into the on open block, where it will run whether the user double-clicks this application, or simply drags/drops files onto it.


I was unable to get the Automator AskforFinderItems.workflow to work when added into the package resource, or script folder. Simply kept telling me that it could not find the (correct) path to the workflow.

Jun 4, 2015 7:51 AM in response to VikingOSX

Sorry to be a pain I found that when I am selecting a folder in the automator section via shell I have an issue with the name of a path


I am navigating to here.

/Users/StudioA/Dropbox/Trash/Weeks 41-42/CompletedFolders/AB_WK41_PSD

but when using the script it says


tell application "System Events"


(*/Users/StudioA/Dropbox/Trash/Weeks*)

Result:

error "Can’t make file \"StudioA:Users:StudioA:Dropbox:Trash:Weeks\" into type alias." number -1700 from file "StudioA:Users:StudioA:Dropbox:Trash:Weeks" to alias

I think it must be the space or the '-' that maybe causing issue?

Choose files(s) or folder and process

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.