Multiline Input

We've made an Applescript that downloads and renames images from our website. It works well, but is limited to one entry at a time. We would like to do multiline input. Ideally the dialog box would give two options (Old Code, New Code) in single window. See attached picture. Or if not possible in Applescript, then two separate dialog boxes is OK.


Here is a list of codes to test:

NMWRAFT-BL

NMWRCNJ-PU

AMBBW

User uploaded file

on run {input, parameters}
    
    set path_to_desktop to path to desktop
    
    set posix_path to POSIX path of path_to_desktop as text
    display dialog "Type PRODUCT CODE" default answer "" buttons {"OK", "Cancel"} default button 1
    
    
    if the button returned of the result is "Cancel" then
        display dialog "Cancelled"
    else
        
        set OLD_code to (text returned of the result)
    end if
    
    display dialog "Paste NEW CODE" default answer "" buttons {"OK", "Cancel"} default button 1
    if the button returned of the result is "Cancel" then
        display dialog "Cancelled"
    else
        
        set NEW_code to (text returned of the result)
    end if
    
    
    set weblink to "'http://img.tenniswarehouse-europe.com/new_big/" & OLD_code & "'-[1-7].jpg"
    
    set curl_command to "curl " & weblink & " -o " & posix_path & NEW_code & "-#1.jpg"
    do shell script curl_command
    
    
    return
    input
end run

null-OTHER, OS X Yosemite (10.10.5)

Posted on Nov 20, 2017 2:16 AM

Reply
Question marked as Top-ranking reply

Posted on Nov 20, 2017 9:49 AM

Your example looks like a mixture of an Automator action and some UI layout application - what are you using?


Another option, depending on exactly what you are looking for, is to use a regular AppleScript dialog set up to get multiple lines, and separate items on each line with a character - for example, something that would be illegal in a filename anyway, such as a colon - and then use text item delimiters to separate the old and new items, for example (input lines are in the form of old_name:new_name and are separated by returns):


on run -- example
  set delimiter to ":"
  set posix_path to POSIX path of (path to desktop)

  repeat with anItem in (getInput for 5 with prompt and title without padding)
    try -- a little error handling
      set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delimiter}
      if (offset of delimiter in anItem) is 0 then error "missing code item"
      set {OLD_code, NEW_code} to text items of anItem
      if OLD_code is "" or NEW_code is "" then error "missing code item"
      set AppleScript's text item delimiters to TempTID

      set weblink to "'http://img.tenniswarehouse-europe.com/new_big/" & OLD_code & "'-[1-7].jpg"
      set curl_command to "curl " & weblink & " -o " & quoted form of (posix_path & NEW_code & "-#1.jpg")
      do shell script curl_command

    on error errmess -- skip errors
      set AppleScript's text item delimiters to TempTID
      log quoted form of anItem & space & space & errmess
    end try
  end repeat
end run


to getInput for inputItems given title:title, prompt:prompt, padding:padding
  (*
  Displays a dialog for multiple item entry - a carriage return is used between each input item.
  For each item in inputItems, a line of text is displayed in the dialog and a line is reserved for the reply.
  To fit the size of the standard dialog, items should be limited in length (~30) and number (~15). 
    parameters -   inputItems [list/integer]: a list or count of items for the dialog
                   title [boolean/text]: use a default or the given dialog title text
                   prompt [boolean/text]: use a default or the given prompt text
                   padding [boolean]: pad/truncate to the count of inputItems
    returns [list]: a list of the input items
  *)
  set {itemList, reply, itemCount} to {{}, {}, (count inputItems)}
  if itemCount is less than 1 then if class of inputItems is integer then -- no item list
    set {itemCount, inputItems} to {inputItems, ""}
  else
    error "getItems handler:  input is empty"
  end if

  if prompt is in {true, false} then if prompt then -- "with" or "without" prompt
    set prompt to "Input items, separated by carriage returns:"
    if inputItems is not "" then set prompt to prompt & return & return
  else
    set prompt to ""
  end if

  if title is in {true, false} then if title then -- "with" or "without" title
    set title to "Multiple Input Dialog" -- default
  else
    set title to ""
  end if

  repeat itemCount times -- set the number of lines for the dialog reply
    set the end of reply to ""
  end repeat
  set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
  set {inputItems, reply} to {inputItems as text, reply as text}
  set AppleScript's text item delimiters to TempTID

  set reply to paragraphs of text returned of (display dialog prompt & inputItems with title title default answer reply)

  if padding is true then -- "with" or "without" padding
    repeat with anItem from 1 to itemCount -- pad/truncate to match inputItems
      try
        set the end of itemList to (item anItem of reply)
      on error
        set the end of itemList to ""
      end try
    end repeat
  else
    repeat with anItem in reply -- just trim to the items entered
      if contents of anItem is not "" then set the end of itemList to anItem
    end repeat
  end if
  return itemList
end getInput
15 replies
Question marked as Top-ranking reply

Nov 20, 2017 9:49 AM in response to adrian_30

Your example looks like a mixture of an Automator action and some UI layout application - what are you using?


Another option, depending on exactly what you are looking for, is to use a regular AppleScript dialog set up to get multiple lines, and separate items on each line with a character - for example, something that would be illegal in a filename anyway, such as a colon - and then use text item delimiters to separate the old and new items, for example (input lines are in the form of old_name:new_name and are separated by returns):


on run -- example
  set delimiter to ":"
  set posix_path to POSIX path of (path to desktop)

  repeat with anItem in (getInput for 5 with prompt and title without padding)
    try -- a little error handling
      set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delimiter}
      if (offset of delimiter in anItem) is 0 then error "missing code item"
      set {OLD_code, NEW_code} to text items of anItem
      if OLD_code is "" or NEW_code is "" then error "missing code item"
      set AppleScript's text item delimiters to TempTID

      set weblink to "'http://img.tenniswarehouse-europe.com/new_big/" & OLD_code & "'-[1-7].jpg"
      set curl_command to "curl " & weblink & " -o " & quoted form of (posix_path & NEW_code & "-#1.jpg")
      do shell script curl_command

    on error errmess -- skip errors
      set AppleScript's text item delimiters to TempTID
      log quoted form of anItem & space & space & errmess
    end try
  end repeat
end run


to getInput for inputItems given title:title, prompt:prompt, padding:padding
  (*
  Displays a dialog for multiple item entry - a carriage return is used between each input item.
  For each item in inputItems, a line of text is displayed in the dialog and a line is reserved for the reply.
  To fit the size of the standard dialog, items should be limited in length (~30) and number (~15). 
    parameters -   inputItems [list/integer]: a list or count of items for the dialog
                   title [boolean/text]: use a default or the given dialog title text
                   prompt [boolean/text]: use a default or the given prompt text
                   padding [boolean]: pad/truncate to the count of inputItems
    returns [list]: a list of the input items
  *)
  set {itemList, reply, itemCount} to {{}, {}, (count inputItems)}
  if itemCount is less than 1 then if class of inputItems is integer then -- no item list
    set {itemCount, inputItems} to {inputItems, ""}
  else
    error "getItems handler:  input is empty"
  end if

  if prompt is in {true, false} then if prompt then -- "with" or "without" prompt
    set prompt to "Input items, separated by carriage returns:"
    if inputItems is not "" then set prompt to prompt & return & return
  else
    set prompt to ""
  end if

  if title is in {true, false} then if title then -- "with" or "without" title
    set title to "Multiple Input Dialog" -- default
  else
    set title to ""
  end if

  repeat itemCount times -- set the number of lines for the dialog reply
    set the end of reply to ""
  end repeat
  set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
  set {inputItems, reply} to {inputItems as text, reply as text}
  set AppleScript's text item delimiters to TempTID

  set reply to paragraphs of text returned of (display dialog prompt & inputItems with title title default answer reply)

  if padding is true then -- "with" or "without" padding
    repeat with anItem from 1 to itemCount -- pad/truncate to match inputItems
      try
        set the end of itemList to (item anItem of reply)
      on error
        set the end of itemList to ""
      end try
    end repeat
  else
    repeat with anItem in reply -- just trim to the items entered
      if contents of anItem is not "" then set the end of itemList to anItem
    end repeat
  end if
  return itemList
end getInput

Nov 20, 2017 8:25 AM in response to adrian_30

set oldcode_list to {"NMWRAFT-BL", "NMWRCNJ-PU", "AMBBW"}

set newcode_list to {"APPLE-RD", "STRAWBERRY-PK", "KIWI"}

try

-- any cancel will send directly to error handler with error number -128

set old_select to (choose from listoldcode_listwith title ¬

"Old Codes" with prompt "Choose one or more Old Codes" default items ¬

"NMWRAFT-BL" with multiple selections allowed without empty selection allowed)


set new_select to (choose from listnewcode_listwith title ¬

"New Codes" with prompt "Choose one or more New Codes" default items ¬

"APPLE-RD" with multiple selections allowed without empty selection allowed)

on error errmsgnumbererrnbr

my error_handler(errnbr, errmsg)

return

end try


if length of old_select = 1 then

-- process one item

else

-- process multiple items in a repeat loop

end if

if length of new_select = 1 then

-- process one item

else

-- process multiple items in a repeat loop

end if

return


on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 5

end error_handler


User uploaded fileUser uploaded file


Here is the AppleScript code to prompt with a list of codes, where the highlighted one is the default choice, but multiple choices are allowed. Clicking cancel will cause the error handler to fire. And finally, you check whether one or more selections were chosen, and process the selection accordingly.


Only one choose from list will display at a time. The above is purely screen captures of individual list queries positioned here for display purposes.

Nov 20, 2017 5:49 AM in response to adrian_30

AppleScript has a very limited choice in visual elements, and these are not extensible with AppleScript alone. AppleScript is single-threaded so you can only have one interface element on screen at a time.


In order to place one visual element on screen that incorporates your two panels, and buttons will require either AppleScript/Objective-C, or separately, Objective-C or Swift programming languages with Xcode. Writing and supporting custom applications of this complexity are out of scope for the intended purposes of the Apple Support Communities — unless someone has too much personal time that they wish to consume coding, testing, and debugging.


I suggest that you explore your local market for a resource that can code and support

this custom solution.

Nov 20, 2017 12:15 PM in response to adrian_30

There isn't a limit for the inputs, my example just creates 5 lines for the dialog - note that the text field used by the dialog doesn't have scroll bars, but you can use arrow keys.


I don't know what your workflow is, but you can change the URLs in the script, or if you have a few known URLs you could add a choose from list to set the URL for a particular set of items.

Nov 20, 2017 6:45 AM in response to VikingOSX

Hey Viking,


Thank you for your information ! How about the version of "one interface element on screen at a time" ?

Something like :

1'st window - codes

2'nd window - new codes


The most important thing for us is to figure out how to do multiple inputs on our code.


If you have the time to check our code you will understand what I mean.


Thank you again !

Nov 20, 2017 4:38 PM in response to rccharles

Those end of line characters (option+L) were deliberately inserted by me for formatting purposes, and work fine in my El Capitan Script Editor. Yes, they can be removed, if one back spaces the following line upwards.


I copied the code that I posted earlier, back into Script Editor, and it compiled and ran without any errors. Also, when I copy/paste back into Script Editor, my code is single-spaced, when pasted, compiled, and run.

Nov 20, 2017 5:48 PM in response to VikingOSX

I haven't pasted applescript code recently. I recall problems with the posted line continuation character. More recently copying text between ASC and textwrangler results in line doubling for me.


Test. Well, I was able to copy & paste & run successfully the following code.


on run
    -- Write a message into the event log.
    log "  --- Starting on " & ¬
        ((current date) as string) & ¬
        " --- "
    log "All done " & ¬
        " for now." 
end run


Above code survived the round trip.


R

PS. Doesn't seem like the line numbering is surviving. Here is what I see in edit mode.

User uploaded file

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Multiline Input

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