Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string and get the first element in AppleScript

Tags:

applescript

I want an AppleScript that does the following:

  • given a string (Kdiff_full_call) as a paramenter
  • split a string and get
  • the first element, which is a path to a zip file
  • unzip the file in the Downloads directory

How can I do that in AppleScript?

Kdiff_full_call has the following pattern: string1-string2 string3 string4

So far I have this script:

on open location kdiff_full_call
    set array to my theSplit(kdiff_full_call, "-")
    -- set string1 ... ??
do shell script "/usr/bin/unzip -d " & quoted form of string1
end open location

on theSplit(theString, theDelimiter)
    -- save delimiters to restore old settings
    set oldDelimiters to AppleScript's text item delimiters
    -- set delimiters to delimiter to be used
    set AppleScript's text item delimiters to theDelimiter
    -- create the array
    set theArray to every text item of theString
    -- restore the old setting
    set AppleScript's text item delimiters to oldDelimiters
    -- return the result
    return theArray
end theSplit
like image 462
letimome Avatar asked Oct 12 '25 18:10

letimome


1 Answers

you were pretty close. in AS an array is a list of items. so you can access it like:

set string1 to array's item 1
-- or
set string1 to item 1 of array

or better if the pattern never changes and you always have the same amount of strings

set {string1,string2,string3,string4} to array

I'd recommend to rename your vars according to their content for better understanding. Instead of string1 I'd take filePath (or whatever)


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!