Tuesday, September 21, 2021

#1 2021-09-08 05:56:23 pm

ale82to
Member
Registered: 2012-09-27
Posts: 39

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:

Applescript:


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.

Offline

 

#2 2021-09-09 06:09:41 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: help with text search and replace

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)

Applescript:


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)

Last edited by KniazidisR (2021-09-09 06:21:03 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-09-09 06:44:03 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5426

Re: help with text search and replace

KniazidisR wrote:

2) replace plain strings with corresponding NSString values (because stringByReplacingOccurrencesOfString method expects NSString values).


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.


NG

Offline

 

#4 2021-09-09 08:14:03 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: help with text search and replace

Nigel Garvey wrote:

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;

Last edited by KniazidisR (2021-09-09 08:16:06 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2021-09-09 08:20:05 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1043

Re: help with text search and replace

ale82to wrote:

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



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:

Applescript:

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)

Last edited by peavine (2021-09-09 09:12:26 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#6 2021-09-09 09:00:53 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5426

Re: help with text search and replace

KniazidisR wrote:

I have not tested


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

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;


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.


NG

Offline

 

#7 2021-09-09 11:21:46 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: help with text search and replace

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.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#8 2021-09-09 11:29:55 am

ale82to
Member
Registered: 2012-09-27
Posts: 39

Re: help with text search and replace

peavine wrote:

thank you very much again!this works perfectly!

ale82to wrote:

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



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:

Applescript:

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)

Offline

 

#9 2021-09-09 01:04:12 pm

ale82to
Member
Registered: 2012-09-27
Posts: 39

Re: help with text search and replace

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

Applescript:

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»

Offline

 

#10 2021-09-09 02:28:46 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1043

Re: help with text search and replace

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:

Applescript:

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

Last edited by peavine (2021-09-09 02:30:50 pm)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#11 2021-09-09 04:39:24 pm

ale82to
Member
Registered: 2012-09-27
Posts: 39

Re: help with text search and replace

peavine wrote:

omg sorry! you r right! thank you very much for your help your script is a real life saviour !!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:

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)