help with text search and replace

hello everyone last week i asked for help with text search and replace and this wonderful apple script almost always works for what i need:


use framework "Foundation"
use scripting additions

set oldString to "<dimen name="mozac_browser_toolbar_url_fading_edge_size">24.0dip</dimen>"

set newString to "<dimen name="mozac_browser_toolbar_url_fading_edge_size">0.0dip</dimen>"


set theFile to "/Users/Al3/app-arm64-v8a-nightly-unsigned/res/values/dimens.xml"

set fileContents to current application's NSString's stringWithContentsOfFile:theFile encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

set fileContents to current application's NSString's stringWithString:fileContents
set fileContents to fileContents's stringByReplacingOccurrencesOfString:oldString withString:newString

(current application's NSString's stringWithString:fileContents)'s writeToFile:theFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

the example i posted above anyway is not working i think beacause of the many double quotes that may "confuses " apple script ,how can i correct the syntax?
thanks for the help.

I think, you should:

  1. escape nested quotes using "" symbol,
  2. replace plain strings with corresponding NSString values (because stringByReplacingOccurrencesOfString method expects NSString values).

(see code lines 3,4,5,6)


use framework "Foundation"
use scripting additions

set oldString to "<dimen name=\"mozac_browser_toolbar_url_fading_edge_size\">24.0dip</dimen>"
set oldString to current application's NSString's stringWithString:oldString

set newString to "<dimen name=\"mozac_browser_toolbar_url_fading_edge_size\">0.0dip</dimen>"
set newString to current application's NSString's stringWithString:newString

set theFile to "/Users/Al3/app-arm64-v8a-nightly-unsigned/res/values/dimens.xml"

set fileContents to current application's NSString's stringWithContentsOfFile:theFile encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

set fileContents to current application's NSString's stringWithString:fileContents
set fileContents to fileContents's stringByReplacingOccurrencesOfString:oldString withString:newString

(current application's NSString's stringWithString:fileContents)'s writeToFile:theFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

Hi.

The ‘fileContents’ string must be an NSString in order to “own” the method. The two parameter texts can be either AppleScript or NSString. They’ll be converted to NSString automatically if necessary.

I have not tested, but this seems to explain the fact that for the OP this code worked with simple strings too. Still, I think it is better not to contradict the documentation of the method, for the future. It clearly states NSString, without any caveats.

Declaration

  • (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
    withString:(NSString *)replacement;

ale82to. I supplied the script you mention and I’m glad it was useful. The script works as originally written but contains unnecessary code. KniazidisR has explained the need to escape the quote characters and so a revised script would be:

use framework "Foundation"
use scripting additions

set oldString to "<dimen name=\"mozac_browser_toolbar_url_fading_edge_size\">24.0dip</dimen>"
set newString to "<dimen name=\"mozac_browser_toolbar_url_fading_edge_size\">0.0dip</dimen>"
set theFile to "/Users/Al3/app-arm64-v8a-nightly-unsigned/res/values/dimens.xml"

set fileContents to current application's NSString's stringWithContentsOfFile:theFile encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

set fileContents to fileContents's stringByReplacingOccurrencesOfString:oldString withString:newString

fileContents's writeToFile:theFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

It’s often best to do so rather than shooting from the hip, especially when giving advice to others.

The Objective-C documentation’s aimed at programmers writing in Objective-C. If you’re writing in AppleScriptObjC, you’re allowed to use the facilities it provides.

Nigel, since I looked at the method’s documentation, that means I didn’t shoot from the hip. I don’t know the intricacies of the English language, but your last comment is unpleasant. In addition, I have already met with cases where AppleScript or AsObjC did not correct the sloppiness of the syntax from the user. So I don’t think my proposal made anything worse.

sorry for bother again the escape mothod works perfectly except for this case:

use framework "Foundation"
use scripting additions

set oldString to "<item name=\"tabCounterTintColor\">?primaryText</item>"
set newString to "<item name=\"tabCounterTintColor\">#ffcd853f</item>"

set theFile to "/Users/Al3/app-arm64-v8a-nightly-unsigned/res/values/styles.xml"

set fileContents to current application's NSString's stringWithContentsOfFile:theFile encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

set fileContents to fileContents's stringByReplacingOccurrencesOfString:oldString withString:newString

when running i ve got this error!!:«class ocid» id «data optr0000000070EC930300600000»

ale82to. You skipped the last line of the script which wrote the string back to the file. If that was your intent, and you just want the script to return the modified string, then you have to do the following:

use framework "Foundation"
use scripting additions

set oldString to "<item name=\"tabCounterTintColor\">?primaryText</item>"
set newString to "<item name=\"tabCounterTintColor\">#ffcd853f</item>"

set theFile to "/Users/Al3/app-arm64-v8a-nightly-unsigned/res/values/styles.xml"

set fileContents to current application's NSString's stringWithContentsOfFile:theFile encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

set fileContents to fileContents's stringByReplacingOccurrencesOfString:oldString withString:newString

set fileContents to fileContents as text