I'm trying to clean up the string so that it removes "//name#" and "deletethispart.file" from "//name#main.domain.com/directory01/directory02/deletethispart.file"
set mainString to "//name#main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name#", "deletethispart.file"}
repeat with i from 1 to count of cleanUpTerms
set text item delimiters to item i of cleanUpTerms
set cleanURL to text items of mainString
set text item delimiters to ""
set mainString to cleanURL
end repeat
It's not working :(
You have to concatenate the text items at the end of the repeat loop.
Renaming cleanURL with textItems makes it clearer
set mainString to "//name#main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name#", "deletethispart.file"}
repeat with i from 1 to count of cleanUpTerms
set text item delimiters to item i of cleanUpTerms
set textItems to text items of mainString
set text item delimiters to ""
set mainString to textItems as text
end repeat
As red_menace said, text item delimiters can be a list. it means you do not need a loop for each delimiter:
set mainString to "//name#main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name#", "deletethispart.file"}
set text item delimiters to cleanUpTerms
set cleanURL to text items of mainString
set text item delimiters to ""
set mainString to cleanURL as text
Related
Ok, first time playing with Applescript today and after 6-7 hours am 'almost' there. Basic premise of this script is that it should read two columns of data out of an Excel spreadsheet, one column is the phone number, the other is an alert. It then writes the message into iMessage/SMS and delivers it.
I have it working off of a CSV well, and now trying to use the source Excel file to bypass the conversion step. I am running into a classic number formatting issue where it reads the number into scientific notation and disrupts the phone number. On converting it back to a normal string iMessage seems to choke on the formatting. (at this point I know its something simple I am missing.)
set phoneCol to "O"
set messageCol to "P"
set startRow to 3
set endRow to 5
set xlsFilePath to (path to desktop as text) & "test.xlsx"
tell application "Microsoft Excel" to open file xlsFilePath
repeat with thisRow from startRow to endRow
tell application "Microsoft Excel"
set targetBuddyPhone to value of cell (phoneCol & thisRow) as string
set targetMessage to value of cell (messageCol & thisRow) as string
end tell
set targetBuddyPhone to number_to_string(targetBuddyPhone)
tell application "Messages"
send targetMessage to buddy targetBuddyPhone of service "SMS"
end tell
delay 2
end repeat
on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_string
by contrast the following scrip to read the same variables from a CSV works with no issues at all, I did need to put a small delay in or I overran the buffer in iMessage.
set theFile to (choose file with prompt "Select the CSV file")
-- read the file contents:
set f to read theFile
-- break the file into paragraphs (c.f. rows)
repeat with row in (paragraphs of f)
-- parse the row into comma-delimited fields
set fields to parseCSV(row as text)
-- now you have your data:
set targetBuddyPhone to item 1 of fields
set targetMessage to item 2 of fields
tell application "Messages"
send targetMessage to buddy targetBuddyPhone of service "SMS"
end tell
delay 2
end repeat
on parseCSV(theText)
set {od, my text item delimiters} to {my text item delimiters, ","}
set parsedText to text items of theText
set my text item delimiters to od
return parsedText
end parseCSV
I need an applescript to find " and replace with just a space. I found an awesome script on here that works beautifully to find and replace whole words (hello, world) but when you manipulate the script to find " and replace with a space, the " corrupts the code and it no longer works. I am hoping somebody knows a way to alter this code to make it do what I want or have an other idea. here is the code (code credit goes to adamh):
searchAndReplaceTextInCells("hello", "world")
on searchAndReplaceTextInCells(search_str, replace_str)
tell application "Microsoft Excel"
set search_range to range "A:Z"
set all_found_ranges to {} -- store for the ranges, to manipulate after searching
set found_range to ""
set counter to 0
try
set found_range to find search_range what search_str with match case
on error
log ("No matches found")
end try
if (found_range is not "") then
set first_cell_address to (get address of the cells of found_range) -- we use this to break our loop
repeat while true
set counter to counter + 1
copy found_range to end of all_found_ranges
-- Now look for next result
set found_range to find next search_range after found_range
set cell_address to (get address of the cells of found_range)
if (cell_address = first_cell_address) then
-- have looped around so we are finished!
exit repeat
end if
end repeat
end if
-- walk all the ranges found and do the string replacing
repeat with r in all_found_ranges
set value of r to my replace_chars(the value of r, search_str, replace_str)
end repeat
log ("found and replaced " & counter & " items")
end tell
end searchAndReplaceTextInCells
on replace_chars(this_text, search_string, replacement_string)
set my text item delimiters to the search_string
set the item_list to every text item of this_text
set my text item delimiters to the replacement_string
set this_text to the item_list as string
set my text item delimiters to ""
return this_text
end replace_chars
As " is a reserved character we need to treat it differently when referencing it in strings.
You could use the quote constant as your argument:
searchAndReplaceTextInCells(quote, " ")
..or you could send it in as an escaped character:
searchAndReplaceTextInCells("\"", " ")
The following livecode for replace the particular text and highlight the both search and replace string. The search string also strike with help of html code. Now i try to add new button that perform delete the search string and place only the replaced string and that do not highlight.
on mouseUp
put the htmlText of field "MytextField" into myHtml
set the caseSensitive to true
put the field SRText into myArrayToBe
split myArrayToBe by CR
put the number of lines of (the keys of myArrayToBe) into myArraylength
repeat with i = 1 to myArraylength
put myArrayToBe[i] into y
split y by colon
put y[1] into searchStr
put y[2] into replaceStr
if searchStr is empty then
put the 0 into m
else
replace searchStr with "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in myHtmlend if
end repeat
set the htmlText of fld "MytextField" to myHtml
end mouseUp
I cannot test your code because I do not have your field contents. But know that the command "replace" requires a container from which the strings may be referenced:
replace searchString with replaceString in fld 1
It will not throw an error, but you do not need to use "the" in such circumstances as:
put the 0 into m (just say "put 0 into m)
put the field SRText into MyArrayToBe (put field SRText into...)
You get the picture.
Anyway, write back with some test text that we might evaluate, or better, make that test yourself with simple text. Then when that works, try it on your real text.
Craig Newman
I have a stack on which there are 3 fields that toggle their colors and their corresponding checkboxes. At the same time the toggling should change the variable (gMyCategories) in order to calculate the correct number of lines and type of categories. The Select All button works OK, but the code for selecting and disselecting each category by toggling the text fields (Short, Medium, Long) does not work - I don't get the correct line numbers and categories in the display fields: "categories" and "Will show".
The code is in the group "fldCat" (see the stack)
global gAllLines,gMyCategories
on mouseUp
set the itemDel to tab
put empty into gMyCategories
repeat with j = 1 to the number of fields of grp "fldCat"
if the backgroundcolor of the target is white then
set the hilite of btn (the short name of the target) of grp "CheckCat" to "true"
put the short name of the target & tab after gMyCategories
set the backgroundcolor of the target to yellow
else
set the backgroundcolor of the target to white
set the hilite of btn (the short name of the target) of grp "CheckCat" to "false"
end if
end repeat
delete char -1 of gMyCategories --tab
put gMyCategories into fld "SelCat"
local LinesInMyCategories
repeat for each line i in gAllLines
if item 3 of i is among the items of gMyCategories then put item 1 of i &tab& item 2 of i & tab & item 3 of i &tab& item 4 of i &cr after LinesInMyCategories --lines in selected categories
end repeat
delete char -1 of LinesInMyCategories --return
put the number of lines in LinesInMyCategories into fld "NrOfLines"
put the number of items in gMyCategories into fld "NrOfCategories"
end mouseUp
What do I have to correct?
see the stack here:
toggle field selection.zip
keram
Give this modified code a try;
global gAllLines,gMyCategories
on mouseUp
set the itemDel to tab
put empty into gMyCategories
# toggle the clicked field
if the backColor of the target = white then
set the backColor of the target to yellow
else
set the backColor of the target to white
end if
# build list of selected
repeat with j = 1 to the number of fields of grp "fldCat"
put the short name of field j of grp "fldCat" into tName
if the backgroundcolor of field j of grp "fldCat" is white then
set the hilite of btn tName of grp "CheckCat" to false
else
put tName & tab after gMyCategories
set the hilite of btn tName of grp "CheckCat" to true
end if
end repeat
delete char -1 of gMyCategories --tab
put gMyCategories into fld "SelCat"
local LinesInMyCategories
repeat for each line i in gAllLines
if item 3 of i is among the items of gMyCategories then put item 1 of i &tab& item 2 of i & tab & item 3 of i &tab& item 4 of i &cr after LinesInMyCategories --lines in selected categories
end repeat
delete char -1 of LinesInMyCategories --return
put the number of lines in LinesInMyCategories into fld "NrOfLines"
put the number of items in gMyCategories into fld "NrOfCategories"
end mouseUp
I am trying to copy the nonblanks cells in my excel file to txt file. My data looks like:
1 2 3
1 2
1
1 2 3 4
So, if i select all and copy, txt file shows like the empty cells have data which is not something that i want.
I tried this:
Crtl+A
Go to special
Constants
Numbers
These commands selects the nonblank cells but I cannot copy them. Is there a way to copy them? I get:
That command cannot be used on multiple selections.
Thanks
If you got notepad++, you can use the regex find and replace to remove all the extra tabs at the end of a line.
Open the txt file in notepad++ and hit Ctrl+H.
In find, put:
\t+$
In replace, leave it blank.
Then check the radio button for the search mode from 'Normal' to 'Regular expression'. After that, hit 'Replace All' and this should be it.
Dim fso As FileSystemObject
Dim stream As TextStream
Dim str As String
Set fso = New FileSystemObject
Set stream = fso.CreateTextFile("c:\myTextFile.txt", True)
For i = 1 To 10
For Each cell In Range("A" & i & "F" & i)
If Not IsEmpty(cell) Then
str = str + cell.Text + " "
End If
Next cell
stream.writeline (str)
str = ""
Next i
stream.Close
End Sub
all you gotta do is change what you wanted separated by ( the " " at the end of the str line) and the range you want ( i = rows 1 through 10 as is) and ("a" & i ":f" & i which indicates a through f, in this case for rows 1 through 10)
hope this helps
With Word as your text editor, copy Excel as is and Paste Special as Unformatted Text. Replace ^t^t with ^t until no more replacements are made, then ^t^p with ^p.