1

I would like to extract name from text list.

Result from do shell shell command:

set Clist to do shell command "ls ./"

Is:

John_01044445558_pass.jpg<br>
Jane_01044562738_visa.jpg<br>
Doe_01027381938_pass.jpg<br>
Joe_7485.jpg

I need John Jane Doe Joe from that list in this format:

John, Jane, Doe, Joe

How can I achieve this?

Here is Full script.

tell application "Finder"
set fullname to name of (selection as alias)
set Cdate to do shell script "date '+%Y.%m.%d'"
set AppleScript's text item delimiters to {"_"}
if fullname contains ".jpg" then
    set CnameNFD to first text item of fullname
    set Cmobile to second text item of fullname
    set Ccustomer to second text item of fullname
    set Ctemp to "/Volumes/homes/macbook/Save/"
    set Clink to "https://r.loyverse.com/dashboard/#/clients/database?page=0&limit=10&search="
    set Cname to ¬
        do shell script "echo " & (CnameNFD as text) & "|iconv -f UTF-8-MAC -t UTF-8"
    --Save폴더에서 전화번호로 검색된리스트를 이름만 가져옴
    --      set Cppl to do shell script "Ctemp=" & Ctemp & " Cmobile=" & Cmobile & ";ls $Ctemp | grep $Cmobile | awk -F'_' '{print $1}' "
    set Cppl to do shell script "Ctemp=" & Ctemp & " Cmobile=" & Cmobile & ";ls $Ctemp | grep $Cmobile "
    set lsOutputAsList to ¬
        paragraphs of ¬
        Cppl
    set theNamesList to {}
    repeat with thisItem in lsOutputAsList
        copy first text item of thisItem ¬
            to the end of theNamesList
    end repeat
    set AppleScript's text item delimiters to ", "
    set theNames to theNamesList as text
    set AppleScript's text item delimiters to {""}
    do shell script "Cname=" & Cname & " Cmobile=" & Cmobile & " Ccustomer=" & Ccustomer & " theNames=" & theNames & "; echo \"'$theNames'\""

else display dialog "올바른 파일을 선택하십시오." buttons {"Okay"} default button 1 end if end tell

If I display dialog theNames it shows as I expected. But I run with shell script echo theNames it shows nothing. I need to use theNames variable to curl post.

4
  • Just an FYI... The $PWD of a do shell script command is / and as such there is no way that the variable Clist in set Clist to do shell command "ls ./" will contain the output you've shown. It will contain a listing of what is in the root of the, e.g., Macintosh HD. In other words, set Clist to paragraphs of (do shell command "ls ./") will be: {"Applications", "Library", "System", "Users", "Volumes", "bin", "cores", "dev", "etc", "home", "opt", "private", "sbin", "tmp", "usr", "var"} Jul 30, 2021 at 0:18
  • The reason why I am do shell command ls ./ is the path is already set to specific folder at the very beginning of the script. I will update full script when I get back home.
    – Curtis.S
    Jul 30, 2021 at 0:36
  • I guess its because of “, ”. If I change it to “,” it works. Then, I can’t use “space after comma” while using shell script?
    – Curtis.S
    Jul 30, 2021 at 9:08
  • You shouldn’t parse the output of ls. It says so in its own man page. Its output can vary considerably between systems, between shells, between versions of macOS, or simply between folders. Instead of do shell script "ls $Ctemp | grep $Cmobile", use do shell script "cd \"$Ctemp\"; printf '%s\n' *\"$Cmobile\"*", or the name of every file in the folder named (Ctemp as POSIX file) whose name contains Cmobile.
    – CJK
    Aug 4, 2021 at 20:44

2 Answers 2

1

I need John Jane Doe Joe from that list in this format. John, Jane, Doe, Joe

How can I achieve this?


The following example AppleScript code assumes the output of your do shell script command is:

John_01044445558_pass.jpg
Jane_01044562738_visa.jpg
Doe_01027381938_pass.jpg
Joe_7485.jpg


Example AppleScript code:

set lsOutputAsList to ¬
    paragraphs of ¬
    "John_01044445558_pass.jpg
Jane_01044562738_visa.jpg
Doe_01027381938_pass.jpg
Joe_7485.jpg"

set theNamesList to {}

set {TID, AppleScript's text item delimiters} to ¬
    {AppleScript's text item delimiters, "_"}

repeat with thisItem in lsOutputAsList
    copy first text item of thisItem ¬
        to the end of theNamesList
end repeat

set AppleScript's text item delimiters to ", "
set theNames to theNamesList as text
set AppleScript's text item delimiters to TID

return theNames

Result: John, Jane, Doe, Joe



What the example AppleScript code is doing:


set lsOutputAsList to ¬
    paragraphs of ¬

Converts the output of the ls command run in a do shell script command to a list instead of carriage return (CR or hex 0D) terminated lines of text. This is done so as to be able to easily loop through and manipulate the output returned.

Note that this is an example and meant as a place holder for:

set Clist to paragraphs of (do shell command "ls ./")

Where the actual do shell script command returns the output shown in the OP, meaning the do shell script command as shown is not really the actual shell command. See my comment to the OP, which is repeated in the Notes: section at the end of this answer.



set theNamesList to {}

Creates a list object to store the name portion of the filenames.



set {TID, AppleScript's text item delimiters} to ¬
    {AppleScript's text item delimiters, "_"}

Uses AppleScript's text item delimiters to create a list of text items from the ensuing filenames, i.e., it coerces, e.g., "John_01044445558_pass.jpg", the contents of thisItem in lsOutputAsList to {"John", "01044445558_pass.jpg"} in the repeat loop.



repeat with thisItem in lsOutputAsList
    copy first text item of thisItem ¬
        to the end of theNamesList
end repeat

For each item in the list, copy the first text item, e.g., "John", in e.g., {"John", "01044445558_pass.jpg"} to the end of the theNamesList list.



set AppleScript's text item delimiters to ", "

Sets up a new text item delimiter to coerce the theNamesList list into a string of text.



set theNames to theNamesList as text

Coerces the theNamesList list, e.g., {"John", "Jane", "Doe", "Joe"} into a string of text to the variable theNames, e.g., "John, Jane, Doe, Joe".



set AppleScript's text item delimiters to TID

As a good practice, restores AppleScript's text item delimiters to the original text item delimiter which by default is {""}. While in some cases, ascertaining the current text item delimiter before modifying it, and restoring it after modifying it may not be programmatically necessary in a specific use case, nonetheless, I refer back to "As a good practice,".



return theNames

For demonstration purposes, returns the value of theNames variable, which in this example is: John, Jane, Doe, Joe



Notes:

Just an FYI... The $PWD of a do shell script command is / and as such there is no way that the variable Clist in set Clist to do shell command "ls ./" will contain the output shown in the OP. It will contain a listing of what is in the root of the, e.g., Macintosh HD. In other words, set Clist to paragraphs of (do shell command "ls ./") will be: {"Applications", "Library", "System", "Users", "Volumes", "bin", "cores", "dev", "etc", "home", "opt", "private", "sbin", "tmp", "usr", "var"}

1

Here is another AppleScript approach which will return the names as you need them.

-- Choose The Folder That Contains  Files With The Underscores In
-- Which The Names Need To Be Extracted

activate
set sourceFolder to quoted form of (POSIX path of (choose folder))

set text item delimiters to ", "
set namesList to paragraphs of (do shell script "find " & sourceFolder & ¬
    " -name '*_*.jpg' -exec basename {} \\; | sed 's/_.*//'") as text
set text item delimiters to ""
2
  • 1
    @user3439894 I can always count on you to scrutinize my code. Good looking out ; )
    – wch1zpink
    Jul 30, 2021 at 23:08
  • I edited your code as -name '*.jpg' -and -name '*_*' was not really necessary since using a single -name primary, e.g. -name '*_*.jpg' does the job. I edited the sed command as the use the -E option was only necessitated by your use of + instead of the default *. Searching for an _ character doesn't require treating a BRE as an ERE. Also changed the pattern delimiters to the default / as there was no real (programatic) need to use an alternate delimiter. Users not familiar with sed may be confused by @ as the sed manual page uses /. Jul 31, 2021 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .