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
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)
Related
I have a string with periods and numbers.
Ex. 4..1...68875....9.....92.7...42.9..6..9...1..6..7.59...8.64.....5....71494...3..2
I am trying to add a comma after each character to get this:
4,.,.,1,.,.,.,6,8,8,7,5,.,.,.,.,9,.,.,.,.,.,9,2,.,7,.,.,.,4,2,.,9,.,.,6,.,.,9,.,.,.,1,.,.,6,.,.,7,.,5,9,.,.,.,8,.,6,4,.,.,.,.,.,5,.,.,.,.,7,1,4,9,4,.,.,.,3,.,.,2
I have about 50 of these lines to convert. Is there a quicker way to do so with AppleScript?
All the searches return how to split with delimiters but I have no idea what terminology I am looking at and what phrase I need to use to get the actual answer.
I just want to add a comma after each character.
Thanks for any help.
Try this:
set myVar to "4..1...68875....9.....92.7...42.9..6..9...1..6..7.59...8.64.....5....71494...3..2"
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {""}}
set myList to text items of myVar as list
set AppleScript's text item delimiters to {","}
set myString to myList as string
set AppleScript's text item delimiters to saveTID
Newbie to AppleScript here, I've learned the following method from other online sources that allows me to extract a string that is between two strings I can define, see code:
to ExText(searchText, startText, endText)
set spaceholder to "x"
set searchText to ("x" & searchText)
set AppleScript's text item delimiters to spaceholder
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to ""
return finalText
end ExText
I'm using this method to extract user's first and last name from an html string obtained by running Javascript id search. I'll do
Set SourceString to "<span>firstname lastname</span>"
ExText(SourceString, "<span>", "</span>")
It works 90% of the time, but in a few instances, depending on the user's name, it'll get an error with this message
Can’t make text items 2 thru -1 of "(part of the name)" into type text.
Here are some sample names that'll break this method
<span>Xu Chang</span>
<span>Maxim Smith</span>
In further testing, I confirmed that any names containing the letter "X" would fail this method. I've examined the actual source strings side by side with the names that contain X and the ones that don't, so it's not the source string but the method itself.
It's worth noting that this error is reproducible everytime.
A rather bizarre behavior. Any ideas?
changing the variable of spaceholder from "x" to "xx" should work
to ExText(searchText, startText, endText)
set spaceholder to "xx"
set searchText to ("xx" & searchText)
set AppleScript's text item delimiters to spaceholder
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to ""
return finalText
end ExText
I have been at this for two days.
So, using Automator and Applescript, I need to scan a volume (or volumes) and get a path to each file, the file name plus extension, assetID (if there is one) and output each part to a comma separated csv file.
So far, I have the Automator actions sorted out and most of the Applescript part but I am at my wits end. The paths and file names work but extracting the assetID (if there is one) is the problem. Not every file has an assetID (and those I am not interested in). The assetID is always a 10 digit number at the end of the file preceded by an underscore("_") like so - afilename_1234567890.ext. As it is now, the script will display the assetID's of the files it processes but as soon as it gets to a file with no id, I see the following error, "The action “Run AppleScript” encountered an error: Can’t get text 1 thru -1 of "".” Something is getting munged somewhere.
Any help would be greatly appreciated.
The script so far...
on run {input, parameters}
-- save delimiters to restore old settings
set savedDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to "/"
repeat with aPath in input
-- set a variable to contain the "/" (POSIX) version of the files path
set filesPath to POSIX path of aPath
-- get the file name
set fileName to last text item of filesPath
-- get the file name without the extension
set thePrefix to text 1 thru ((offset of "." in fileName) - 1) of fileName
-- get the asset ID, if there is one
set assetID to rightStringFromRight(thePrefix, "_")
display dialog assetID
if (class of assetID) is integer then
-- get the path only
set pathOnly to ((text items 1 thru -2 of (get POSIX path of aPath)) as Unicode text) & "/"
-- output the path only, file name and asset ID to a comma delimited csv file
display dialog assetID
end if
end repeat
-- restore the old delimiter setting
set AppleScript's text item delimiters to savedDelimiters
end run
on rightStringFromRight(str, del)
local str, del, oldTIDs
set oldTIDs to AppleScript's text item delimiters
try
set str to str as string
if str does not contain del then return str
set AppleScript's text item delimiters to del
set str to str's last text item
set AppleScript's text item delimiters to oldTIDs
return str
on error eMsg number eNum
set AppleScript's text item delimiters to oldTIDs
error "Can't rightStringFromRight: " & eMsg number eNum
end try
end rightStringFromRight
on is_number(number_string)
try
set number_string to number_string as number
return true
on error
return false
end try
end is_number
I finally got the script working. A few wrong assumptions corrected and all is well.
I'm trying to get an AppleScript to find somme keyword from the clipboard and list them in a new clipboard
e.g : I copy this line in the clipboard "order KAFGEFEF price 999 date 17 order KADFSDGS price 874 date 18"`
and the result will be
K1AFGE2FEF
K1ADFSD2GS
or even beter
K1AFGE2FEF : 999
K1ADFSD2GS : 17
the data I want to collect always start with "K1...." and have 10 characteres.
I actually had a old Javascript which are kind of doing the trick but I need to use AppleScript instead.
I really not sure where to start here, maybe I shoud start something around pbcopy and egrep ?
hope that's make sense.
Kind regards.
It is not clear from your question exactly how your clipboard data is structured or what your desired output is. For starters, here is an Applescript solution that will extract order, price, and date values from the clipboard. It assumes that order, price, and date are always grouped together in that specific order, and that there can be multiple order-price-date groups in a single line of text on the clipboard. For example:
order K1AFGE2FEF price 999 date 17 order K1ADFSD2GS price 874 date 18
Then the following Applescript will extract each order, price, and date triplet and save it as a three-item sublist in a master list:
set masterList to {}
set tid to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to "order "
repeat with i in (get (the clipboard)'s text items 2 thru -1)
tell i's contents
try
set currOrder to first word
set AppleScript's text item delimiters to "price "
set currPrice to (get its text item 2)'s first word
set AppleScript's text item delimiters to "date "
set currDate to (get its text item 2)'s first word
if (currOrder starts with "K1") and (currOrder's length = 10) then set end of masterList to {currOrder, currPrice, currDate}
end try
end tell
end repeat
end try
set AppleScript's text item delimiters to tid
return masterList -- {{"K1AFGE2FEF", "999", "17"}, {"K1ADFSD2GS", "874", "18"}}
The master list can then be processed further into whatever output you desire.
I have the following code, which prompts user for a comma separated list of places:
set AppleScript's text item delimiters to {","}
set thePlaces to the text items of the text returned of (display dialog "Insert referenced places separated by commas" default answer "")
This will result on a list with several items ("Paris","London", ...).
My intention is to prefix every item of this list with a string (for eg. "plc:".
In the end, I would like the list to be composed by items such as:
"plc:Paris", "plc:London".
Have been trying but with no luck so far. Can anyone point me in the right direction?
Thanks.
Looks a little brutal, but works as wished:
repeat with i from 1 to count thePlaces
set item i of thePlaces to "plc:" & item i of thePlaces
end repeat
The repeat loop loops through the items and add "plc:" in front of the content...
Enjoy, Michael / Hamburg
This is how you can do it with text item delimiters, we box each item, with a unique value for the front, and one for the end, so that we can differentiate between the two. No use for this with so small lists, really. I just wanted to show you how it can be done.
set astid to text item delimiters
set the places to "Paris,London,Rome"
set text item delimiters to ","
set lstItms to text items of the places
-- we "box" the text items, so that every one is prepended with a return, and has a linefeed appended to it.
set text item delimiters to return & linefeed
set places to lstItms as text
set text item delimiters to astid
set places to linefeed & places & return
-- our list is in shape, time to do the actual replacement.
set text item delimiters to linefeed
set lstItms to text items of places
set text item delimiters to "plc:"
set places to lstItms as text
set text item delimiters to return
set lstItms to text items of places
set text item delimiters to astid
log item 1 of lstItms
(*plc:Paris*)