Replacing spaces in filename to pass to Clipboard with applescript - text

I am working on a script to have Finder get a file's location locally, replace spaces with %20, then append localhost:// so the transformed file name can be inserted into email.
I've have used the replacing spaces code structure suggested elsewhere on stackoverflow but the code is coming up with error 1721. I am not sure what I've done wrong.
The code for text substitution is:
on run {input, parameters}
set newString to {"localhost://"}
set aString to "/file name input/"
set aString to aString as text
set charToReplace to " "
set newChar to "%20"
repeat with i in aString
if (i as string) is charToReplace then
set end of newString to newChar
else
set end of newString to (i as string)
end if
end repeat
return input
end run
The output should be /file%20name%20input/
Thanks for any help you can offer.
Michael

Not sure on that 1721 error, but here is an alternate method for string search and replacing.
It uses AppleScript's text item delimiters. I think of this technique akin to the split().join() method to replace text in JavaScript.
set aString to "/file name input/"
set my text item delimiters to " "
set split_list to every text item of aString -- split in to list of everything between the spaces
set my text item delimiters to "%20"
set newString to (split_list as text) -- join, using the %20 as the delimter
set newString to "localhost://" & newString -- prepend your protocol string

Here is a standard version of a search and replace sub-routine for Applescript:
set encodedString to searchAndReplace(aString, space, "%20")
on searchAndReplace(myString, oldText, newText)
set AppleScript's text item delimiters to oldText
set myList to text items of myString
set AppleScript's text item delimiters to newText
set myString to myList as string
set AppleScript's text item delimiters to ""
return myString
end searchAndReplace

Related

How can i get each line of this string with VB6

I want to know how I can get each line of this string until the next #.
#cnb Santander
#cnn VENTA
#br
#cnn PRUEBAS INTEGRACIONES DLL
so I can have it on different line like:
#cnb Santander
#cnn VENTA
I have tried using Split function like this:
Dim sLines() As String, L As Long
sLines = Split(Text1.Text, vbCrLf)
but it seems that vbCrLf is not working
also tried:
sLines = Split(Text1.Text, " ")
If the text was manually entered into a multiline TextBox, then each press of Return will add a CrLf combination and your code should work just fine.
Since the code doesn't work, I assume that the text is being loaded from somewhere else, and that the likely problem is that the text is Unix formatted, meaning that it only contains Lf characters, not CrLf. (I've also come across Cr on it's own before too.)
If this is the case, then you simply need to convert the line feeds first. I usually convert everything to single Lf characters when splitting like this...
Dim sLines() As String
Dim sTemp As String
sTemp = Replace$(Text1.Text, vbCrLf, vbLf) ' Convert CrLf combinations to Lf only
sTemp = Replace$(sTemp, vbCr, vbLf) ' Convert any remaining Cr's to Lf's
sLines = Split(sTemp, vbLf) ' Spilt on Lf instead of CrLf

Getting text from webpage

I asked and read different topic about this in the last month, but still didn't get it
I want to extract some data from a website using Applescript or something simular
With this script
to getInputByClass2(theClass, num) -- defines a function with two inputs, theClass and num
tell application "Safari" --tells AS that we are going to use Safari
set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1 -- uses JavaScript to set the variable input to the information we want
end tell
return input --tells the function to return the value of the variable input
end getInputByClass2
-- start here
getInputByClass2("field type-string field-accountname", 0)
set theText to Unicode text
set theText to getInputByClass2("field type-string field-accountname", 0)
I have this result :
"<div class=\"content\"><div class=\"related-orders-indicator component\" id=\"XXXX-BBBB-CCCC-DDDD-EEEE\" data-bad=\"0\" data-good=\"51\" data-total=\"51\">
<label class=\"count\" style=\"display: block;\">51</label>
</div><label class=\"name\">accountname</label><span class=\"value\">1985</span><div class=\"confirmation\"><input type=\"checkbox\" class=\"tri-state confirmation square-confirmation-style\"></div></div>"
How can I have only the account name as a variable, in this example 1985.
Many thanks in advance
You can do it with text item delimiters:
property leftEdge : "class=\"value\">"
property rightEdge : "</span>"
set theSource to "<div class=\"content\"><div class=\"related-orders-indicator component\" id=\"XXXX-BBBB-CCCC-DDDD-EEEE\" data-bad=\"0\" data-good=\"51\" data-total=\"51\"><label class=\"count\" style=\"display: block;\">51</label></div><label class=\"name\">accountname</label><span class=\"value\">1985</span><div class=\"confirmation\"><input type=\"checkbox\" class=\"tri-state confirmation square-confirmation-style\"></div></div>"
set saveTID to text item delimiters
set text item delimiters to leftEdge
set classValue to text item 2 of theSource
set text item delimiters to rightEdge
set theResult to text item 1 of classValue
set text item delimiters to saveTID
theResult

Convert AppleScript List into String

I was wondering if there is a short way to convert an AppleScript list into a string separating each item. I can achieve this in a way which is more lengthy that I would like, so I wondered if there is a simple way to achieve this. Basically, I would like to take a list such as {1,2,3} and convert it in to a string "1, 2, 3". I can do something like below, but that results in a comma following the resulting string:
set myList to {"1.0", "1.1", "1.2"}
set Final to ""
if (get count of myList) > 1 then
repeat with theItem in myList
set Final to Final & theItem & ", "
end repeat
end if
There is a short way, it's called text item delimiters
set myList to {"1.0", "1.1", "1.2"}
set saveTID to text item delimiters
set text item delimiters to ", "
set Final to myList as text
set text item delimiters to saveTID
Create your own snippet for this
Convert a list to a string is so frequent that you better create a subroutine.
on list2string(theList, theDelimiter)
-- First, we store in a variable the current delimiter to restore it later
set theBackup to AppleScript's text item delimiters
-- Set the new delimiter
set AppleScript's text item delimiters to theDelimiter
-- Perform the conversion
set theString to theList as string
-- Restore the original delimiter
set AppleScript's text item delimiters to theBackup
return theString
end list2string
-- Example of use
set theList to {"red", "green", "blue"}
display dialog list2string(theList, ", ")
display dialog list2string(theList, "\n")

AppleScript and .txt file

I need to have an AppleScript that will edit the contents of a given text file (generic structure) and delete the 5th character through 8th character, leave characters 9-20, and delete characters 21-32. For example:
Say this is my text file:
"
Qt&:$yp$shshshahahah$jsjsjajssjh
"
(Single line)
I would need to delete starting from the first $ to the next $, and then delete everything after (including) the last $. In this example, the end result would be this:
Qt&:shshshahahah
Thanks,
Isaac D'Keefe
You could run a command like this in Terminal:
sed -Ei '' 's/(.{4}).{4}(.{12}).*/\1\2/' ~/Documents/file
Or if you need to use AppleScript:
set p to "/Users/username/Documents/file"
set input to read p as «class utf8»
set input to text 1 thru 4 of input & text 9 thru 20 of input
set fd to open for access p with write permission
set eof fd to 0
write input to fd as «class utf8»
close access fd
I would need to delete starting from the first $ to the next $, and
then delete everything after (including) the last $. In this example,
the end result would be this:
Based on these conditions you can split an string up into chunks using a delimiter/separator. Then to my understanding you only want to keep the odd items and remove the even ones (by index). So the following script will work as you described rather than working on the index of the characters.
set theString to " Qt&:$yp$shshshahahah$jsjsjajssjh "
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$"}
set textItems to every text item of theString
set AppleScript's text item delimiters to oldTID
set filteredTextItems to {}
repeat with x from 1 to count textItems by 2
set end of filteredTextItems to item x of textItems
end repeat
return filteredTextItems as string

How can I add commas to a string of numbers in Applescript?

In Applescript I have a string of single digit numbers:
0123456789
I want to go through that string and add a comma between each number, so it would show:
0,1,2,3,4,5,6,7,8,9
How can I do this with Applescript?
Notes
I want both the input and output to be of type "string" - not list.
The numbers will always be single digit numbers.
You can use a handy feature called AppleScript's text item delimiters, which allows you to break up (or "parse" in computer jargon) text into segments, and then extract data from those segments. They are the separators of text items in a piece of text. A simple example:
set prevTIDs to AppleScript's text item delimiters
set theString to "Don't-eat-the-yellow-snow"
set AppleScript's text item delimiters to "-" --tell AppleScript to break up strings at each occurrence of a hyphen '-' in a given string
set these_items to every text item of theString
//--> {"Don't", "eat", "the", "yellow", "snow"}
set AppleScript's text item delimiters to prevTIDs
return these_items
It is always considered good practice when working with text item delimiters to restore the original delimiters; once you change the delimiters, they will globally affect the running environment until the process is closed and restarted.
Another use for text item delimiters is replacing words or characters in a given string:
set prevTIDs to AppleScript's text item delimiters
set theString to "Don't eat the yellow snow"
set AppleScript's text item delimiters to "yellow" --the word you want to replace
set temp to every text item of theString
//--> {"Don't eat the", "snow"}
set AppleScript's text item delimiters to "pink" --the replacement word
set theString to temp as string
set AppleScript's text item delimiters to prevTIDs
return theString
//--> "Don't eat the pink snow"
However, note that this replaces every occurrence of the word "yellow". The reason I'm saying this is consider the following string:
If someone added one plus one, what would be the result?
If you wanted to replace the word "one" with the word "two", you would have to be sure to precede "two" with a space when creating the new delimiter, or your resulting string would be the following:
If sometwo added two plus two, what would be the result?
What you're trying to do is basically replacing empty strings with commas. All you need to do is follow these simple steps to do this:
Create a variable to store the current delimiters in
Create a variable to store your string in
Change the delimiter to an empty string ""
Coerce your string into a list (i.e. set the list to every text item of yourString)
Change the delimiter to a comma ,
Coerce your newly created list back into a string (i.e. set yourString to list as string)
Restore the old delimiters
return your string
The resulting code:
set prevTIDs to AppleScript's text item delimtiers
set myString to "0123456789"
set AppleScript's text item delimiters to ""
set the_list to every text item of myString
set AppleScript's text item delimiters to ","
set myString to the_list as string
set AppleScript's text item delimiters to prevTIDs
return myString
Happy coding! :)
Something like this?
set theNumber to "3452678190"
set AppleScript's text item delimiters to ","
set theItems to every character of theNumber as string
set AppleScript's text item delimiters to ""
return theItems

Resources