set a to "My Name is Stefan, and I am a total applescript noob"
set b to "noob"
set noobgone to ""
set OldDelims to AppleScript's AppleScript's text item delimiters
set AppleScript's AppleScript's text item delimiters to "noob"
set newA to text items of a
set AppleScript's AppleScript's text item delimiters to ""
set newA to newA as text
set AppleScript's AppleScript's text item delimiters to OldDelims
return newA
I’d suggest something similar to what pidge1 posted:
set a to "My Name is Stefan, and I am a total applescript noob"
set a to simpleReplace("noob", "", a)
on simpleReplace(search, replace, subject)
local search, replace, subject, ASTID
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to search
set subject to text items of subject
set AppleScript's text item delimiters to replace
set subject to "" & subject
set AppleScript's text item delimiters to ASTID
return subject
end simpleReplace
I read from a file - there are 2000 lines - with the text delimiter I identify one
line and delete it (from the variable) - with the code you have built for me!
When I read the variable again I see that there is an empty line where I
previous deleted the “b” - Is it possible to not only delete but also make
something like a “delete line”?
set a to "My Name is Stefan, and I am an applescript noob
master after going on the forums."
set a to simpleReplace("noob
", "", a)
on simpleReplace(search, replace, subject)
local search, replace, subject, ASTID
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to search
set subject to text items of subject
set AppleScript's text item delimiters to replace
set subject to "" & subject
set AppleScript's text item delimiters to ASTID
return subject
end simpleReplace
The problem with this is that it won’t replace occurances of the word when it doesn’t have a newline after it. Instead, you may want to just add a second pass where you search for two newlines in a row and replace them with a single one. This would eliminate any empty lines.
thanks a lot for your help - It is working fine - but not exactly what should happen…
I will show you the results:
first the code:
set delete_nachname to "Lehrner"
set delete_vorname to "Laetitia"
set delete_telefon to "+436505800550"
set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to (read theFile)
display dialog del_list
set a to del_list
set a to simpleReplace(delete_adressbook, "", a)
on simpleReplace(search, replace, subject)
local search, replace, subject, ASTID
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to search
set subject to text items of subject
set AppleScript's text item delimiters to replace
set subject to "" & subject
set AppleScript's text item delimiters to ASTID
return subject
end simpleReplace
the problem is, you replace the line, but not the line break.
I would do it a different way
set delete_nachname to "Lehrner"
set delete_vorname to "Laetitia"
set delete_telefon to "+436505800550"
set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to paragraphs of (read theFile)
repeat with i from 1 to count del_list
if item i of del_list is delete_adressbook then
set del_list to delete_list_item(del_list, i)
exit repeat
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set del_list to del_list as Unicode text
set text item delimiters to TID
display dialog del_list
on delete_list_item(theList, theItem)
if theItem is 1 then
return rest of theList
else if theItem is (count theList) then
return items 1 thru -2 of theList
else
return (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList)
end if
end delete_list_item
Considering that you’re going to process approx. 2000 lines,
I recommend a binary search instead of parsing the list serially
set delete_nachname to "Lehrner"
set delete_vorname to "Laetitia"
set delete_telefon to "+436505800550"
set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to paragraphs of (read theFile)
set foundIndex to BinarySearch(del_list, delete_adressbook)
if foundIndex is not false then set del_list to delete_list_item(del_list, foundIndex)
set {TID, text item delimiters} to {text item delimiters, return}
set del_list to del_list as Unicode text
set text item delimiters to TID
display dialog del_list
on delete_list_item(theList, theItem)
if theItem is 1 then
return rest of theList
else if theItem is (count theList) then
return items 1 thru -2 of theList
else
return (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList)
end if
end delete_list_item
on BinarySearch(theList, theValue)
local low, mid, high
script o
property l : theList
end script
set low to 1
set high to (count theList)
repeat while (low ≤ high)
set mid to (low + high) div 2
if ({theValue} is in items low thru mid of o's l) then
set high to mid - 1
else
set low to mid + 1
end if
end repeat
if (item low of o's l is theValue) then return low
return false
end BinarySearch
using the first example - I have written this - do you think that my syntax is correct?
(Sorry, I have no modem here at the moment, so I am not able to test)
if name of theObject is "bt_delete_adressbook" then
set delete_nachname to contents of text field "nachname" of drawer "Inbox" of window "main"
set delete_vorname to contents of text field "vorname" of drawer "Inbox" of window "main"
set delete_telefon to contents of text field "telefon" of drawer "Inbox" of window "main"
set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
set script_path to ((path to desktop as Unicode text) & "adressbook_delete.scpt")
set del_list to load script file script_path
tell delete_list_item
set del_list to delete_list_item(theList, theItem)
end tell
set write_adressbook to del_list
-- hier schreiben wir ins file - alles ausser dem gelöschten namen
set fileToCheck_adressbook to ((path to desktop as Unicode text) & "adressbook.txt")
set eof of fileToCheck_adressbook to 0
set open_adressbook to open for access fileToCheck_adressbook with write permission
write (write_adressbook as string) to open_adressbook starting at eof
close access open_adressbook
-- hier wird wieder gelesen
delay 1
--hier lesen wir wieder aus dem file:
set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set num_list to paragraphs of (read theFile)
set myList_ab to {}
set {tid, text item delimiters} to {text item delimiters, ";"}
repeat with num in num_list
set end of myList_ab to text items 1 thru 3 of num
end repeat
set text item delimiters to tid
set content of table view "adressbook" of scroll view "adressbook" of drawer "Inbox" of window "main" to myList_ab
end if
delete_adressbook.scpt looks like this:
set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to paragraphs of (read theFile)
repeat with i from 1 to count del_list
if item i of del_list is delete_adressbook then
set del_list to delete_list_item(del_list, i)
exit repeat
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set del_list to del_list as Unicode text
set text item delimiters to TID
display dialog del_list
on delete_list_item(theList, theItem)
if theItem is 1 then
set q to rest of theList as text
else if theItem is (count theList) then
set q to items 1 thru -2 of theList as text
else
set q to (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList) as text
end if
end delete_list_item
What are the advantages of the binary search comparing to the first one?
I don’t know, because I don’t have your environment, so I can’t test it either.
Run it and look, what’s happening.
SPEED!
If you have a list of 1000 items, it can take (worst case) 1000 loops to find the item (serial search).
With binary search it takes at most 10 loops to find it (2^10 = 1024)