I'm making an Automator App that will run an AppleScript. The app will be used for running a Terminal script on a file which the input receives – both by launching the app or by dropping a file on the app's icon. When launching the app, user can select a file manually. However, when user drops multiple files onto the app, I want it to choose only the first file of those files, because the script used later can only work with one single file at a time.
Below is the code I have until now. When one or multiple files are dropped on the app, I get an error: "Can't get item 1 of 1."
Any advice would be greatly appreciated. Thank you
on run {input, parameters}
if input is {} then
set inputFile1 to (POSIX path of (choose file with prompt "Please select a file to process:"))
end if
if input is not {} then
set inputFile1 to {}
repeat with i from 1 to count input
set end of inputFile1 to quoted form of (POSIX path of (first item of i))
end repeat
end if
tell application "Terminal"
if not (exists window 1) then reopen
activate
do script "xxx" & inputFile1 in window 1
end tell
end run
Best Answer
The following example AppleScript code is the minimum coding to achieve the goal presented in the OP, that is to only act on the first file dropped on the Automator created AppleScript application using a single Run AppleScript action:
echo
in thedo script
command for testing purposes, instead ofxxx
as in the OP.Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g.
delay 0.5
, with the value of the delay set appropriately.