Thursday, October 17, 2019

#1 2019-10-07 07:49:44 am

NMG
Member
Registered: 2019-09-29
Posts: 11

Manipulate text with Applescript and perhaps regex

I have this as a result of a script:

/path/to/document.html (some-text) — Brackets

I need only: /path/to/document.html

Be aware that the path of the document could be any url and inside the brackets could be any text.

I know a bit of regex and I tried here: https://regexr.com/4mabg
I know almost nothing about the terminal and all the solutions I found on Google seem related to that and I just do not understand. Is there any easy to learn solution?


Filed under: text, regex

Offline

 

#2 2019-10-07 08:33:14 am

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

Re: Manipulate text with Applescript and perhaps regex

Generally text item delimiters are used to do what you want. For example:

Applescript:

set theItem to "/path/to/document.html (some-text) — Brackets"

set ATID to AppleScript's text item delimiters -- save existing TID
set AppleScript's text item delimiters to {" ("} -- set TID to " ("
set thePath to text item 1 of theItem
set AppleScript's text item delimiters to ATID -- set TID to saved TID

thePath

The following contains introductory information on text item delimiters:

https://macscripter.net/viewtopic.php?id=24422

An alternative, but microscopically slower, approach uses the offset command:

Applescript:

set theItem to "/path/to/document.html (some-text) — Brackets"

set thePath to text 1 thru ((offset of " (" in theItem) - 1) of theItem

Last edited by peavine (2019-10-07 10:33:00 am)


2018 Mac mini - macOS Mojave

Offline

 

#3 2019-10-07 10:37:05 am

NMG
Member
Registered: 2019-09-29
Posts: 11

Re: Manipulate text with Applescript and perhaps regex

Thank you very much. It works very well and the link you provide explains it very well also. Only one question: do I need this line?
set ATID to AppleScript's text item delimiters

This works well:
set theItem to "/path/to/document.html (some-text) — Brackets"
set AppleScript's text item delimiters to {" ("} -- set new TID's
set thePath to text item 1 of theItem
set AppleScript's text item delimiters to theItem

Offline

 

#4 2019-10-07 11:19:41 am

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

Re: Manipulate text with Applescript and perhaps regex

NMG wrote:

Only one question: do I need this line?
set ATID to AppleScript's text item delimiters


The direct answer to your question is no. However, it is a generally-accepted practice to save/reset TIDs as done in my script in order to avoid breaking something later on. If you do decide to delete that line, you should also delete or modify the last line of my script.

NMG wrote:

This works well:

set theItem to "/path/to/document.html (some-text) — Brackets"
set AppleScript's text item delimiters to {" ("} -- set new TID's
set thePath to text item 1 of theItem
set AppleScript's text item delimiters to theItem


The last line of the above code sets the TID to:

"/path/to/document.html (some-text) — Brackets"

I can't think of any reason to do this. Instead, IMO, you should 1) delete that line, 2) modify it to set the TID to some value that makes sense within the context of your script, or 3) modify it to set the TID to the default value of "".

Last edited by peavine (2019-10-10 09:38:00 am)


2018 Mac mini - macOS Mojave

Offline

 

#5 2019-10-07 11:20:19 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 618

Re: Manipulate text with Applescript and perhaps regex

NMG wrote:

Thank you very much. It works very well and the link you provide explains it very well also. Only one question: do I need this line?
set ATID to AppleScript's text item delimiters


Hi, NMG.
Yes, you need following 2 code lines always, to preserve the default AppleScript behaviour of your Mac for text manipulations:

Applescript:


set ATID to AppleScript's text item delimiters -- save existing (default) TID
-- Any text manipulations with text item delimiters
set AppleScript's text item delimiters to ATID -- set TID to saved TID (returning to default TID)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#6 2019-10-07 04:52:15 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 902

Re: Manipulate text with Applescript and perhaps regex

The need to preserve the global text delimiter property is a holdover belief—akin to a superstition. Prior to Snow Leopard, there may have been a rationale to preserve TIDS, however, there have been functional changes that make retaining them nonessential. It has always been setting them explicitly that's important.

Offline

 

#7 2019-10-07 05:43:31 pm

maro
Member
From:: Nerima, Tokyo, Japan
Registered: 2004-05-30
Posts: 29
Website

Re: Manipulate text with Applescript and perhaps regex

I think this is one of the regexp question. Shane's routine will be a suitable answer.

Applescript:


-- Created 2019-04-18 by Shane Stanley
-- From his book "Everyday AppleScriptObjC"
-- https://macosxautomation.com/applescript/apps/everyday_book.html
use AppleScript version "2.5" --macOS 10.11 or later
use scripting additions
use framework "Foundation"

property NSString : a reference to current application's NSString
property NSRegularExpression : a reference to current application's NSRegularExpression

set aURLText to "/Users/narcis/Dropbox/feines/mig-basic/ordinador/solucions/prova.html (mig) — Brackets"
set regExpStr to "(.*).\\(.*\\) —.Brackets"

set sheetsID to my findPattern:regExpStr inString:aURLText capturing:1



-- find Grep matches, specifying capture number (0 = all) --by Shane Stanley
on findPattern:thePattern inString:theString capturing:n
   set theNSString to NSString's stringWithString:theString
   set theNSRegularExpression to NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
   set findsNSArray to theNSRegularExpression's matchesInString:theNSString options:0 range:{location:0, |length|:theNSString's |length|()}
   set theFinds to findsNSArray as list -- so we can loop through
   set theResult to {} -- we will add to this
   set theNSString to NSString's stringWithString:theString
   
   repeat with i from 1 to count of items of theFinds
       set anNSTextCheckingResult to (item i of theFinds)
       if (anNSTextCheckingResult's numberOfRanges()) as integer < (n + 1) then
           set end of theResult to missing value
       else
           set theRange to (anNSTextCheckingResult's rangeAtIndex:n)
           set end of theResult to (theNSString's substringWithRange:theRange) as string
       end if
   end repeat
   
   return theResult
end findPattern:inString:capturing:

Model: MacBook Pro 2012
AppleScript: 2.7
Browser: Safari 13.0.1
Operating System: macOS 10.14


I wrote thousands of AppleScript to realize my idea. Natural language interface, voice recognition commander and so on. Though my mother toungue is strange language, Japanese, my most frequently write language is AppleScript. I believe it is for making things easy and powerful.


Filed under: regexp

Offline

 

#8 2019-10-07 08:25:48 pm

technomorph
Member
Registered: 2017-12-14
Posts: 124

Re: Manipulate text with Applescript and perhaps regex

With ObjC. 
Your can get a NSRange of a NSRegularExpression in a NSString
The use stringByReplacingStringInRange:withString:

And use the foundRange for the replacement
And use “” for The replacement

Offline

 

#9 2019-10-07 11:34:37 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5958

Re: Manipulate text with Applescript and perhaps regex

Marc Anthony wrote:

The need to preserve the global text delimiter property is a holdover belief—akin to a superstition. Prior to Snow Leopard, there may have been a rationale to preserve TIDS, however, there have been functional changes that make retaining them nonessential. It has always been setting them explicitly that's important.



There was no change to how text item delimiters behave in Snow Leopard -- what changed was the way Script Editor behaves. Before then, Script Editor used a single AppleScript component instance for all scripts, which meant they all shared delimiters. That was changed so that each script uses its own component instance, and in fact creates a new one each time you compile. And each component instance has its own text item delimiters property.

However, if you intend to run your scripts from another host -- say, a script panel in InDesign, or some other app's scripts menu, or FastScripts  -- it's most likely that that the host will run them all within the same component instance, so they will share the global text item delimiters property.

Last edited by Shane Stanley (2019-10-07 11:35:17 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#10 2019-10-08 04:01:25 am

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

Re: Manipulate text with Applescript and perhaps regex

Marc Anthony wrote:

The need to preserve the global text delimiter property is a holdover belief—akin to a superstition.


It's a philosophy rather than a superstition. A handler which uses the TIDs should leave them in the state they were when it received them, which may not necessarily be the default. This is particularly true with handlers in libraries or collaborative projects, where the philosophy and assumptions of the person writing the calling code aren't known. It's the same courtesy principle as preserving the clipboard contents or closing files you've opened for access. A process which calls a handler should be able to assume that the values it set itself, and didn't specifically call the handler to change, are still the same afterwards.

On the other hand, as with all things in life, one should proceed "courteously but defensively" and not assume anything when using other people's code. So setting the TIDs explicitly before each use is also a good idea.

Not tidying up after yourself is fine if you only write simple scripts for your own use, write all the code yourself, and (as Shane's pointed out) run the results in their own AppleScript instances. There's also something to be said for not going into too much detail when posting example code to scripting fora.  wink  But generally speaking, "courteously but defensively" is a good way to go.


NG

Online

 

#11 2019-10-08 08:30:55 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 902

Re: Manipulate text with Applescript and perhaps regex

I'm in the philosophical camp that it's precisely because the conditions aren't evident that preserving them potentiates a problem and is inadvisable. Let’s say you live in a mixed sex household and wake in the dark of night for a call of nature. Do you assume what the last person may have done or do you set the lid in the desired use position? Similarly, it’s up to the coder to explicitly enforce conditions rather than assume the environment.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)