简体   繁体   中英

Applescript Returned Text not Splitting

Ive been playing with applescript for about 2 weeks now, but I have hit a problem Im trying to create an applescript that reads all the names of the folders on our server. Then displays them in a drop down menu so that I can select the client. The problem I have is that it is displaying the result as one selection option as a big sentence and is not separating each client, so they can be selected individually. so far I have:

set theFolder to alias "server:"
tell application "Finder"
    set theText to name of theFolder & return
    set k to 0
    repeat with thisSubfolder in (get folders of theFolder)
        set k to k + 1
        set theText to theText & name of thisSubfolder & return
    end repeat
end tell
set l to {theText}
set l2 to ""
repeat with i in l
    set l2 to l2 & quoted form of i & " "
end repeat
do shell script "/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog \\
     standard-dropdown --title Title --text Text --items " & l2
set {button, answer} to paragraphs of result
if button is 1 then 
    return item {answer + 1} of l
end if

Many thanks

D

When you do this:

set l to {theText}

You're just creating a list of one item (your string), which means you end up with this:

{"theFolder
folder1
folder2
folder3
"}

You're then repeating in that "list," trying to add spaces between the items. But, you don't have a list really. You have a one item list, with return-delimited strings.

The best way to get a list of folder names would be to get them from System Events . Notice that in this case, you have to create a list with the name of the first folder as the only item. Otherwise, the & operation will join everything together as a string, instead of creating a list.

tell application "System Events"
    set l to (name of theFolder as list) & name of folders of theFolder
end tell

There are also some syntactical issues that will hurt you later:

1 != "1"

CocoaDialog returns a string, with the button number: "1" . You are using if button is 1 . For equality, it should be if button is "1" .

Parentheses are used for grouping, not brackets

If button is "1", you are returning item {answer + 1} of l . I blame Applescript for letting this work when it shouldn't. You're actually creating a list with a number, which then gets coerced by Applescript for the list index. Here are all the steps, assuming answer is 0:

  1. item {answer + 1} of l gets turned into
  2. item {1} of {folder1, folder2, folder3}
  3. Applescript coerces to item 1 of {folder1, folder2, folder3}
  4. Value returned: folder1

Here is a fully updated version of your script:

set theFolder to alias "server:"
tell application "System Events"
    set l to {name of theFolder} & name of folders of theFolder
end tell

set args to ""
repeat with i from 1 to (count l)
    set args to args & quoted form of item i of l
    if i < (count l) then
        set args to args & " "
    end if
end repeat

do shell script "/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog \\
     standard-dropdown --title Title --text Text --items " & args

set {button, answer} to paragraphs of result
if button is "1" then
    return item (answer + 1) of l
end if

Change the line:

set l to {theText}

to:

set l to paragraphs of theText

and you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM