Thursday, December 13, 2018

#1 2018-12-04 09:40:05 am

John Pennick
Member
Registered: 2014-11-17
Posts: 4

Applescript text delimeters

I need to trunctate a file name to rename a JPEG at creation ie

AA00~1234_Rest of heading.pdf

1234.jpg

I have managed it with two underscores or two tildes, courtesy of mg, how can I alter this to allow for the ~ (tilde) and an underscore.

on getChars(t)
    set {oTid, text item delimiters} to {text item delimiters, "~"}
    set l to text items of t -- split text using tilde as the separator, the result is a list of items
    if (count l) > 1 then -- At least 1 tilde
        --set charsAfter1_ to (items 3 thru -1 of l) as text --  the text after the 1st tilde
        set charsBetween1_ to item 2 of l -- the text between the 1st and 2nd tilde
        set t to charsBetween1_ --& "~" & charsAfter3_ -- concatenate one string
    end if
    set text item delimiters to oTid -- reset the text item delimiters
    return t
end getChars
set jpgName to my getChars(mgName)

Thanks for any advice


Filed under: Text Delimeters

Offline

 

#2 2018-12-04 09:58:49 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11526
Website

Re: Applescript text delimeters

In this case I'd recommend to use offset rather than text item delimiters

Applescript:

on getChars(t)
   set tildeOffset to offset of "~" in t
   set underscoreOffset to offset of "_" in t
   if underscoreOffset is 0 then set underscoreOffset to (count t) + 1
   return text (tildeOffset + 1) thru (underscoreOffset - 1) of t
end getChars


regards

Stefan

Offline

 

#3 2018-12-04 10:34:59 am

robertfern
Member
Registered: 2011-11-29
Posts: 32

Re: Applescript text delimeters

Here is a short version using multiple Text item delimiters

Applescript:

on getChars(t)
   set {oTid, text item delimiters} to {text item delimiters, {".", "~", "_"}}
   set l to text items of t -- split text using 3 delimiters, the result is a list of items
   set text item delimiters to oTid -- reset the text item delimiters
   return item 2 of l & "." & item 4 of l
end getChars

set mgname to "AA00~1234_Rest of heading.pdf"
set jpgName to my getChars(mgname)

It uses 3 delimiters to break up the text in one shot

Offline

 

#4 2018-12-04 11:48:39 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3290

Re: Applescript text delimeters

@robertfern

It seems that you missed a point.
The original asker want to rename its file as a jpeg.

Would be edited as :

Applescript:

on getChars(t)
   set {oTid, text item delimiters} to {text item delimiters, {"~", "_"}}
   set l to text items of t -- split text using 3 delimiters, the result is a list of items
   set text item delimiters to oTid -- reset the text item delimiters
   return item 2 of l & ".jpg"
end getChars

set mgname to "AA00~1234_Rest of heading.pdf"
set jpgName to my getChars(mgname)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 4 d├ęcembre 2018 18:47:57

Offline

 

#5 2018-12-05 12:55:48 am

John Pennick
Member
Registered: 2014-11-17
Posts: 4

Re: Applescript text delimeters

Thanks to all, they all work and I see what fits into my scenario better

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)