Tuesday, January 23, 2018

#1 2017-12-12 08:28:08 am

torombolo
Member
Registered: 2008-05-28
Posts: 3

Rename text files with text from file

Hello,

I have some text files with the same structure but different values.

Example:

"Name": "JUAN"; "Apellido": "Garcia"

We need the file to take the value between Name: and Apellido:, so it has to be JUAN.txt.

Thanks in advance.

Offline

 

#2 2017-12-13 06:00:36 am

EricLipton
Member
Registered: 2012-12-02
Posts: 20

Re: Rename text files with text from file

Hi,

try this:

set x to {"Name: JUAN; Apellido: Garcia"} as string
set a to offset of ";" in x
set y to characters 1 thru (a - 1) of x as string
set z to text ((offset of " " in y) + 1) thru -1 of y
set def to z & ".txt" as string



Kind regards, Eric

Offline

 

#3 2017-12-15 07:00:14 am

torombolo
Member
Registered: 2008-05-28
Posts: 3

Re: Rename text files with text from file

Thanks for your answer

Do you think it will work for all my documents? I mean, I need it to search for "Name: XXXXX ; in all my documents and take XXXXX as the name for each document. Obviously, XXXXX is unique for each one.

Thanks

Offline

 

#4 2017-12-16 03:52:08 am

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

Re: Rename text files with text from file

torombolo wrote:

Example:

"Name": "JUAN"; "Apellido": "Garcia"

We need the file to take the value between Name: and Apellido:, so it has to be JUAN.txt.


Hi. Welcome to MacScripter.

It's a bit confusing because in your example the individual words are all in quotes, while in the following line they're not. If the example's correct — and if the "Name": and "Apellido": are the first or only ones in the text — the text between them can be extracted very simply like this:

Applescript:

set txt to "\"Name\": \"JUAN\"; \"Apellido\": \"Garcia\""

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"\"Name\": \"", "\"; \"Apellido\":"}
set theName to text item 2 of txt
set AppleScript's text item delimiters to astid

set fileName to theName & ".txt" --> "JUAN.txt"

Otherwise, if the words aren't actually quoted, just leave the quote characters out of the script:

Applescript:

set txt to "Name: JUAN; Apellido: Garcia"

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"Name: ", "; Apellido:"}
set theName to text item 2 of txt
set AppleScript's text item delimiters to astid

set fileName to theName & ".txt" --> "JUAN.txt"


NG

Offline

 

#5 2017-12-17 03:58:10 pm

EricLipton
Member
Registered: 2012-12-02
Posts: 20

Re: Rename text files with text from file

Hi,

if i understand well, this is what you're looking for. Pick one and try it on a couple of documents:

-- based on words
set macPath to choose file
set posPath to POSIX path of macPath
set searchWord to "Name"
set theContent to do shell script "textutil -stdout -convert txt " & quoted form of posPath
repeat with i from 1 to (count words of theContent)
    if word i of theContent is "Name" then set x to i
end repeat
tell application "Finder" to set name of macPath to ((word (x + 1) of theContent) & ".txt") as string


or

-- based on paragraphs
set macPath to choose file
set posPath to POSIX path of macPath
set searchWord to "Name"
set theContent to do shell script "textutil -stdout -convert txt " & quoted form of posPath
repeat with everyParagraph in paragraphs of theContent
    if searchWord is in everyParagraph then set theName to word 2 of everyParagraph
end repeat
tell application "Finder" to set name of macPath to (theName & ".txt") as string



kind regards,

Eric

Last edited by EricLipton (2017-12-18 06:52:43 am)

Offline

 

#6 2017-12-21 02:38:46 am

torombolo
Member
Registered: 2008-05-28
Posts: 3

Re: Rename text files with text from file

Yes, that's what I meant, sorry if I didn't explained it correctly.

I'll try that script.

Thanks.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)