Sample of ROT13 using tr from the command line

SuperCard 4.5 & 4.6 projects (.zip archives only please).

Sample of ROT13 using tr from the command line

Postby sctell » Tue Oct 15, 2013 8:47 am

I had a private message from drpitcairn about a rot13 external and found I think you can do it using tr from the command line.

Here is my sample project.

translate.sc45.zip
(2.11 KiB) Downloaded 858 times
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Tue Oct 15, 2013 10:31 am

Thanks for doing this. After downloading your project and testing it, seeing it worked fine, and then checking that I had created basically the same project and same script — I realized the problem had to do with my text file — and that is the case.
Starting with the script::
Code: Select all
on mouseup
put cd fld 1 into tText
put merge("echo '[[tText]]' | tr 'A-Za-z' 'N-ZA-Mn-za-m'") into tCommand
put shell(tCommand) into cd fld 2
end mouseup

Here is an example of the sort of information being processed (of a total of several hundred of these entries).
Abies Canadensis
COMMON NAME: HEMLOCK SPRUCE
GENERAL INDICATIONS: Mucous membranes are affected by Abies-c. and gastric symptoms are most marked, and a catarrhal condition of the stomach is produced. There are peculiar cravings and chilly sensations that are very characteristic, especially for women with uterine displacement, probably due to defective nutrition with debility. Respiration and heart action labored. Wants to lie down all the time; skin cold and clammy, hands cold; very faint. Right lung and liver feel small and hard. Gleet. [Br.]
_____________________________________
Cravings (1) for coarse foods, meats, pickles, radish, turnips, etc. [Br.]
The most characteristic symptoms seem to be craving for meat, pickles and other coarse food, a gnawing, hungry, faint feeling at the epigastrium [G.]
Great appetite; canine hunger far beyond one's capacity for digestion [Br.]
Burning in stomach.
Gnawing, faint, hungry feeling (2) at epigastrium [Br.]
Flatulent distension of stomach and abdomen, with severe palpitation; flatulence disturbs heart's action.
Prolapsus uterus and other displacements, tendency to; due to debility and general defective nutrition (Alet., Calc-p., Caul., Helon., Lac-d., Nat-m.) [F.]
Prostration, patients want to lie down all the time [Br.]
CHILLINESS very characteristic.
Feels as if the top of the head congested, with faintness [Al.]
Desire for turtellio since ablactation.
DOSAGE: 1st to 3rd potency.
_____________________________________
(1) Craving refers to an abnormal desire, not a nutritional need but an unusual desire to take in these foods.
(2) A hungry feeling may be one that arises even though enough food has been taken in.

When I try to run this sample text, then nothing appears in the destination field and there is no error message and trace does not show any activity from the script.
So trying smaller portions, I can get this much to work just fine.
Abies Canadensis
COMMON NAME: HEMLOCK SPRUCE
GENERAL INDICATIONS: Mucous membranes are affected by Abies-c. and gastric symptoms are most marked, and a catarrhal condition of the stomach is produced. There are peculiar cravings and chilly sensations that are very characteristic, especially for women with uterine displacement, probably due to defective nutrition with debility. Respiration and heart action labored. Wants to lie down all the time; skin cold and clammy, hands cold; very faint. Right lung and liver feel small and hard. Gleet. [Br.]
_____________________________________
Cravings (1) for coarse foods, meats, pickles, radish, turnips, etc. [Br.]
The most characteristic symptoms seem to be craving for meat, pickles and other coarse food, a gnawing, hungry, faint feeling at the epigastrium [G.]

However, if I add one more line, then there is no output, just an empty destination field.
I had thought at first that it had to do with the brackets or the lines, but that does not seem to be the case. It is almost like there is a memory limitation.
This is all new to me, so I could use some hints as to what may be going on here.

Thanks,
— Richard
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Tue Oct 15, 2013 10:46 am

Tested another document and same limitation. Will only process a limited amount of text. I assume there is a way around this. A repeat loop?
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby codegreen » Tue Oct 15, 2013 8:38 pm

drpitcairn wrote:Tested another document and same limitation. Will only process a limited amount of text. I assume there is a way around this. A repeat loop?


It's failing because your text includes a single quote (which is also being used as a delimiter in the shell script). That's why I sent you a version which operates on files (included here for posterity).

Code: Select all
on ROT13 inFile, outFile
  get shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[hfsToPosix(inFile)]]` > `[[hfsToPosix(outFile)]]`"))
end ROT13

-Mark
codegreen
 
Posts: 1517
Joined: Mon Jul 14, 2008 11:03 pm

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Wed Oct 16, 2013 9:20 am

How can I learn more about using the command line? Is there a book? Other resources?
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby sctell » Wed Oct 16, 2013 10:04 am

A quick search of the internet will turn up lots of resources.

It's probably not necessary to buy anything, it's all out there.

This looks a good start:

http://acad.coloradocollege.edu/dept/pc/SciCompLab/UnixTutorial/index.html

Good luck

Terry
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Wed Oct 16, 2013 11:35 am

Mark,
I have been working the last couple of hours to get your script to perform. I know that I have to call the command for rot13 and put the data into it and get it back out but the terminology is so unfamiliar to me that I have not been able to do it. I thought this should work, but does not.
Code: Select all
on mouseup
  put cd fld 1 into tText
  rot13(tText)
  put it into cd fld 2
end mouseup

But this does not work.

Then I tried using it as a function:
Code: Select all
on mouseup
  put cd fld 1 into tText
  put rot13(tText) into cd fld 2
end mouseup
_______________________________

function ROT13 inFile, outFile
  get shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[hfsToPosix(inFile)]]` > `[[hfsToPosix(outFile)]]`"))
  return newText
end ROT13

But no go with this either.
I am sure it is just a matter of a special word but I have not seen how to do it yet.
Suggestions welcome.
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby sctell » Wed Oct 16, 2013 10:44 pm

Hi Richard,

With regard to Marks function:

on ROT13 inFile, outFile
get shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[hfsToPosix(inFile)]]` > `[[hfsToPosix(outFile)]]`"))
end ROT13

Mark has provided a solution that works with the text contained within a file rather than text stored in SC.

In the ROT13 function above inFile is the path to the file containing the text to have ROT13 applied.
outFile is the path to a file to contain the converted contents from file inFile.

inFile needs to exist as that contains your text. outFile will be created if it does not already exist.

You need to decide where inFile and outFile will reside on your computer and construct the paths. (see the language guide entry below).

The file paths need to be constructed using HFS file format as HFStoPosix converts it. Here is the language guide entry:

The HFStoPOSIX function returns a valid POSIX path corresponding to the HFS path supplied in HFSpath.
For example:
"Macintosh:Users:se:Desktop:MySCProj.sc45"
would return:
"/Users/se/Desktop/MySCProj.sc45"
COMMENTS
SuperCard uses the HFS format to refer to file paths. The HFStoPOSIX() and POSIXtoHFS() functions make it simple to translate file path references to/from other processes that use the POSIX format (such as SuperCard's shell() function).


so,

1. make a text file containing your text.
2. Decide where to store it on your computer
3. Create a path to it similar to put "Macintosh:Users:se:Desktop:myTextFile.txt" into inFile
4. Create a path for outFile similar to put "Macintosh:Users:se:Desktop:myOutFile.txt" into outFile
5. ROT13 inFile,outFile
6. BINGO, outFile or "Macintosh:Users:se:Desktop:myOutFile.txt" contains the converted text
7. Do with it what you will from this point.


All the best

Terry
sctell
 
Posts: 1128
Joined: Sun Jul 06, 2008 10:41 am

Re: Sample of ROT13 using tr from the command line

Postby codegreen » Thu Oct 17, 2013 3:57 am

Hi Richard,

My example isn't a function (at least not in SuperTalk parlance), it's a message handler. In other words it's syntactically equivalent to a command:

Code: Select all
ROT13 "Macintosh HD:Users:moi:Desktop:test.txt", "Macintosh HD:Users:moi:Desktop:test13.txt"

If you just want to return the encoded contents of a file directly, that's easy too:

Code: Select all
function ROT13 inFile
  return shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[hfsToPosix(inFile)]]`"))
end ROT13

The point is that just passing your data IN via a file sidesteps potential string delimiter conflicts (which aren't a concern on the return trip). You can safely return it either way, whichever is more convenient...

-Mark
codegreen
 
Posts: 1517
Joined: Mon Jul 14, 2008 11:03 pm

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Thu Oct 17, 2013 11:37 am

Terry and Mark,
I really appreciate the help, trying to learn this stuff. I have been studying a unix tutorial which helps but a ways to go.

I tried this script:
Code: Select all
on mouseup
  put "Macbook Pro:Users:drpitcairn:Desktop:rltest.txt" into inFile
  put "Macbook Pro:Users:drpitcairn:Desktop:rltest13.txt" into outFile
  ROT13 inFile, outFile
end mouseup
_______________________________

on ROT13 inFile, outFile
  get shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[hfsToPosix(inFile)]]` > `[[hfsToPosix(outFile)]]`"))
end ROT13

And this did access the external test file and then produce the output file. So great.

Then I tried it like this:
Code: Select all
on mouseup
  ROT13 "Macbook Pro:Users:drpitcairn:Desktop:rltest.txt", "Macbook Pro:Users:drpitcairn:Desktop:rltest13.txt"
end mouseup
_______________________________

on ROT13 inFile, outFile
  get shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[hfsToPosix(inFile)]]` > `[[hfsToPosix(outFile)]]`"))
end ROT13

And that also worked fine. So I think I understand how to use this now.

However, what I want to do with this project is have my text file in a userprop rather than as external file. To date, I have been using an external that imports the external file and does the rotate, and puts it in the userprop, and of course I do this just the one time. Then when the program runs and first opens, the userprop text file is accessed and converted/rotated back into readable text and put into a global variable that is accessed during the time the program is used.

So I can see how I could do this with an external file, but how do I need to modify it to read this userprop file? I assume I have to change this part of the script to point it to a variable? That is, I put the userprop file into a variable and then do the rotation? And put the rotated file back into the same variable?

on ROT13 inFile, outFile
get shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[hfsToPosix(inFile)]]` > `[[hfsToPosix(outFile)]]`"))
end ROT13
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby codegreen » Thu Oct 17, 2013 1:03 pm

drpitcairn wrote:So I can see how I could do this with an external file, but how do I need to modify it to read this userprop file? I assume I have to change this part of the script to point it to a variable? That is, I put the userprop file into a variable and then do the rotation? And put the rotated file back into the same variable?

Read the encoded data from the userprop, write that to a temp file, pass its path to tr, then delete it.

Code: Select all
on test
  define the testprop of this proj
  set the testprop of this proj to "Jung'f hc qbp?"
  put loadProp13("testprop", long name of this proj)
end test

function loadProp13 propName, objName
  put line 1 of shell("mktemp ${TMPDIR}tmp.XXXXXXXXXX") into posixPath
  put posixToHFS(posixPath) into hfsPath
  open file hfsPath
  write the propName of objName to file hfsPath
  close file hfsPath
  return shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[posixPath]]`; rm `[[posixPath]]`"))
end loadProp13

In real life you might want to just reuse the same temp file over and over for multiple properties, or make the property parent object reference hard-coded or implicit, but this should give you the basic idea...

HTH,
-Mark
codegreen
 
Posts: 1517
Joined: Mon Jul 14, 2008 11:03 pm

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Thu Oct 17, 2013 1:34 pm

Whew! OK, homework time. Will see how much I can understand of this.
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Thu Oct 17, 2013 4:53 pm

Well, I surprised myself but I got it to work. Here is the script with my comments to see I understand what is happening with it.

on mouseUp
put loadProp13("testprop", long name of this proj) into tremData
-- "testprop" is the name of the userprop; long name...is the path to that userprop.
put tremData into cd fld 2
end mouseUp

function loadProp13 propName, objName
put line 1 of shell("mktemp ${TMPDIR}tmp.trawData") into posixPath
put posixToHFS(posixPath) into hfsPath
-- I don't understand all the words but this is creating a path to the variable, trawData?

open file hfsPath
write the propName of objName to file hfsPath
-- copies the userprop content into this variable.
close file hfsPath
return shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[posixPath]]`; rm `[[posixPath]]`"))
-- returns the rotated text to the variable created above, tremData which is then placed in cd fld 2.
end loadProp13

Do I have it right (sort of)?
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Thu Oct 17, 2013 5:16 pm

The other part of this I have been working on is the import of the external text file into the userprop. This is not done often but as I do more editing of that file and add information to it, then I will import the file, freshly, into the userprop for use by the project.
So I set up a separate script for that purpose and it seems to work.

on mouseUp
put "Macbook Pro:Users:drpitcairn:Desktop:rltest.txt" into inFile
-- establishes the path to the external file.
set the testprop of this proj to ROT13(inFile)
-- calls the function and sets the userprop to it.
end mouseUp
_______________________________

function ROT13 inFile
return shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[hfsToPosix(inFile)]]`"))
-- this is what you gave me before for accessing the external file. It seems to work but I am used to a script that opens and reads the file. This somehow does the same?
end ROT13
_______________________________

See anything I should change? Or is OK?
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby codegreen » Thu Oct 17, 2013 7:46 pm

Hi Richard,

When you don't understand what a script does (or how it does it), remember the magic word trace.

Code: Select all
put line 1 of shell("mktemp ${TMPDIR}tmp.trawData") into posixPath

A few things to note:

- $TMPDIR is a shell environment variable that your script inherits automagically. It contains the POSIX path to a special hidden folder in which you can safely write temporary files.

- Wrapping curly braces around shell variable names removes ambiguity about how to parse them in situations like this where other text butts right up against them in a script.

- mktemp is a shell command that uses a caller-supplied template to generate a temporary file name which is guaranteed to be unique.

Together these construct a POSIX path you can use to create to a temp file without worrying about collisions. The shell() function will return this with a carriage return appended, so getting line 1 of the returned value strips that off.

Code: Select all
 put posixToHFS(posixPath) into hfsPath

POSIX paths are fine for shell commands, but SC uses HFS paths internally, so we have to convert this temporary path to HFS format to use it directly in SuperTalk.

Code: Select all
open file hfsPath
write the propName of objName to file hfsPath
close file hfsPath

This creates the temp file, opens it, writes the uprop data to it, and closes it again. Note that indirection is permitted in both the property name and parent object reference of a user property descriptor.

Code: Select all
 return shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[posixPath]]`; rm `[[posixPath]]`"))

This shell script reads the temp file, converts and returns the data inside, then deletes the file. It uses the shell '<' operator to redirect standard input to our temp file when executing the tr command. Standard input is a pipe many shell commands implicitly try to read data from (unless you specify otherwise).

HTH,
-Mark
codegreen
 
Posts: 1517
Joined: Mon Jul 14, 2008 11:03 pm

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Fri Oct 18, 2013 8:18 am

Mark,
Extraordinarily helpful. Thanks for taking the time.
I was able to follow most of your explanation but a couple of places confused me.

CODE: SELECT ALL
open file hfsPath
write the propName of objName to file hfsPath
close file hfsPath

This creates the temp file, opens it, writes the uprop data to it, and closes it again. Note that indirection is permitted in both the property name and parent object reference of a user property descriptor.
I don't know what the word "indirection" implies. Is it meaning that the name will be given by another part of the script?

CODE: SELECT ALL
return shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[posixPath]]`; rm `[[posixPath]]`"))
In this particular project I have I want to load the text file from the userprop when opening the project and keep it around for use during the time is is open. This is done by putting the file into a global variable. So to continue doing this, would I change the script line like this?
return shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[posixPath]]`"))
put it into tfileHolder

This shell script reads the temp file, converts and returns the data inside, then deletes the file. It uses the shell '<' operator to redirect standard input to our temp file when executing the tr command. Standard input is a pipe many shell commands implicitly try to read data from (unless you specify otherwise).


Thanks again. You have saved my project.
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona

Re: Sample of ROT13 using tr from the command line

Postby codegreen » Fri Oct 18, 2013 9:38 am

drpitcairn wrote:I don't know what the word "indirection" implies. Is it meaning that the name will be given by another part of the script?

In this case it means you can pass the property name and/or parent object reference in variables. This avoids the need to construct statements that operate on arbitrary properties in strings and execute them using 'do' (which is very inefficient, plus it's a pain in the neck).

In this particular project I have I want to load the text file from the userprop when opening the project and keep it around for use during the time is is open. This is done by putting the file into a global variable. So to continue doing this, would I change the script line like this?
return shell(merge("tr 'A-Za-z' 'N-ZA-Mn-za-m' < `[[posixPath]]`"))
put it into tfileHolder

I don't think I understand the question. FWIW anything that follows a return command will not be executed. All I think you need to do is just something like:

Code: Select all
global <aGlobalVariable>
put loadProp13(<propName>, <parentObject>) into <aGlobalVariable>

BTW in case you didn't see it, I also stuck an XFcn up in the files area which should work on PPC. If it doesn't, please let me know...

-Mark
codegreen
 
Posts: 1517
Joined: Mon Jul 14, 2008 11:03 pm

Re: Sample of ROT13 using tr from the command line

Postby codegreen » Fri Oct 18, 2013 9:52 am

BTW I just noticed you changed my example to read:

Code: Select all
put line 1 of shell("mktemp ${TMPDIR}tmp.trawData") into posixPath

which I then obliviously quoted back. Don't do that! The original version:

Code: Select all
put line 1 of shell("mktemp ${TMPDIR}tmp.XXXXXXXXXX") into posixPath

(or at least something structurally similar) is what should be used.

The mktemp command replaces the 'X' chars in the supplied template with other characters to generate a unique filename. No 'X's, no unique filename! If you'd traced through the original and your modified version (like you should have! Tsk tsk...) you'd probably have spotted that error yourself.

;-)
-Mark
codegreen
 
Posts: 1517
Joined: Mon Jul 14, 2008 11:03 pm

Re: Sample of ROT13 using tr from the command line

Postby drpitcairn » Fri Oct 18, 2013 10:24 am

I will change it back. You know I have used trace many times but when it comes to these shell scripts I don't see much that I can understand. My limitation. Here is a picture of what I see. I understand the path to the file with objectname but the other lines I don't.
I can see that line 1 which becomes the hfsPath has put a temporary name for the file (now that you have explained it to me).

trace.gif
trace window
trace.gif (41.06 KiB) Viewed 23102 times
User avatar
drpitcairn
 
Posts: 1023
Joined: Thu Apr 02, 2009 9:39 am
Location: Sedona, Arizona


Return to Sample & Demo Projects

Who is online

Users browsing this forum: No registered users and 1 guest

cron