Thursday, September 2, 2010

#1 2010-03-07 03:42:00 pm

Charlie Hartley
Member
Registered: 2007-04-11
Posts: 40

Combining Multiple (Similar) Scripts?

Using a lot of help, both here and elsewhere, I've come up with the following script that works on the specific file named in it (blm_active.txt). Now I need to do the same thing to five other files located in the same folder (bms_active.txt, ems_active.txt, hms_active.txt, mwm_active.txt, and zms_active.txt). Short of creating five more scripts, each only modified to change the file name, is there an easier way?

Here's the script.

Applescript:


property searchList : {"
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

LOCK TABLES `blm_active` WRITE;
/*!40000 ALTER TABLE `blm_active` DISABLE KEYS */;
INSERT INTO `blm_active` VALUES ("
, ");
/*!40000 ALTER TABLE `blm_active` ENABLE KEYS */;
UNLOCK TABLES;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

"
, "),(", "'"}

property replaceList : {"", return, return, ""}

to switchText of t from s to r
   set text item delimiters to s
   set t to t's text items
   set text item delimiters to r
   tell t to set t to beginning & ({""} & rest)
   t
end switchText

to convertText(t)
   set d to text item delimiters
   considering case
       repeat with n from 1 to count searchList
           set t to switchText of t from my searchList's item n to my replaceList's item n
       end repeat
   end considering
   set text item delimiters to d
   t
end convertText

set currFile to alias "Macintosh HD:Users:chartley:Desktop:Academic League:ovmsal_storage:blm_active.txt"

set openFile to open for access currFile with write permission
set oldText to read openFile

set newText to convertText(oldText)

set eof openFile to 0
write newText to openFile
close access openFile

Thanks,
Charlie

Offline

 

#2 2010-03-07 04:47:51 pm

Hans-Gerd Classen
Member
Registered: 2010-02-19
Posts: 27

Re: Combining Multiple (Similar) Scripts?

i guess you mean another loop.
if all files in the folder should be processed you could just list the folder, so there would be no need to give the specified files into the code ...

Applescript:


property searchList : {"
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

LOCK TABLES `blm_active` WRITE;
/*!40000 ALTER TABLE `blm_active` DISABLE KEYS */;
INSERT INTO `blm_active` VALUES ("
, ");
/*!40000 ALTER TABLE `blm_active` ENABLE KEYS */;
UNLOCK TABLES;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

"
, "),(", "'"}

property replaceList : {"", return, return, ""}

to switchText of t from s to r
   set text item delimiters to s
   set t to t's text items
   set text item delimiters to r
   tell t to set t to beginning & ({""} & rest)
   t
end switchText

to convertText(t)
   set d to text item delimiters
   considering case
       repeat with n from 1 to count searchList
           set t to switchText of t from my searchList's item n to my replaceList's item n
       end repeat
   end considering
   set text item delimiters to d
   t
end convertText

--replace these with yours:
set currFileList to {alias "Macintosh HD:Users:hans:Desktop:file01.txt", alias "Macintosh HD:Users:hans:Desktop:file02.txt", alias "Macintosh HD:Users:hans:Desktop:file03.txt"}
repeat with MyCurrFile from 1 to count of currFileList
   set currFile to item MyCurrFile of currFileList
   set openFile to open for access currFile with write permission
   set oldText to read openFile
   
   set newText to convertText(oldText)
   
   set eof openFile to 0
   write newText to openFile
   close access openFile
end repeat


Hans
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
AppleScript amateur with just basically english skills – so please be patient … ;-)

Offline

 

#3 2010-03-07 05:28:50 pm

Charlie Hartley
Member
Registered: 2007-04-11
Posts: 40

Re: Combining Multiple (Similar) Scripts?

That would work fine if the find/replace text remained constant, but it changes with each file (the filename listed within the find/replace texts is different in each case).

My guess is that this will require six different scripts, so another question might be, can multiple scripts be combined within a single script application, so that clicking on that application would run all six scripts?

Thanks,
Charlie

Offline

 

#4 2010-03-08 08:13:12 am

Matt-Boy
Member
Registered: 2005-10-21
Posts: 428

Re: Combining Multiple (Similar) Scripts?

I think a loop is what you want. Don't forget a list can contain lists!

I didn't test this but it should be close. It loops through (in this case) 3 search, replace, and file lists and processes them as you had it.

Good luck!

Applescript:

--This looks awkward but it is a list of 3 searchlists which currently have the same content

property searchList : {¬
   {"
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

LOCK TABLES `blm_active` WRITE;
/*!40000 ALTER TABLE `blm_active` DISABLE KEYS */;
INSERT INTO `blm_active` VALUES ("
, ");
/*!40000 ALTER TABLE `blm_active` ENABLE KEYS */;
UNLOCK TABLES;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

"
, "),(", "'"}, ¬
   {"
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

LOCK TABLES `blm_active` WRITE;
/*!40000 ALTER TABLE `blm_active` DISABLE KEYS */;
INSERT INTO `blm_active` VALUES ("
, ");
/*!40000 ALTER TABLE `blm_active` ENABLE KEYS */;
UNLOCK TABLES;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

"
, "),(", "'"}, ¬
   {"
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

LOCK TABLES `blm_active` WRITE;
/*!40000 ALTER TABLE `blm_active` DISABLE KEYS */;
INSERT INTO `blm_active` VALUES ("
, ");
/*!40000 ALTER TABLE `blm_active` ENABLE KEYS */;
UNLOCK TABLES;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

"
, "),(", "'"} ¬
       }

--List of the 3 replace lists
property replaceList : {{"", return, return, ""}, {"", return, return, ""}, {"", return, return, ""}}

to switchText of t from s to r
   set text item delimiters to s
   set t to t's text items
   set text item delimiters to r
   tell t to set t to beginning & ({""} & rest)
   t
end switchText

to convertText(t, theItemCounter)
   set d to text item delimiters
   considering case
       repeat with n from 1 to count (item theItemCounter of searchList)
           set t to switchText of t from (item theItemCounter of searchList)'s item n to (item theItemCounter of replaceList)'s item n
       end repeat
   end considering
   set text item delimiters to d
   t
end convertText

--List of the 3 files
set currFileList to {alias "Macintosh HD:Users:hans:Desktop:file01.txt", alias "Macintosh HD:Users:hans:Desktop:file02.txt", alias "Macintosh HD:Users:hans:Desktop:file03.txt"}

repeat with CurrFileCounter from 1 to count of currFileList
   set currFile to item CurrFileCounter of currFileList
   set openFile to open for access currFile with write permission
   set oldText to read openFile
   
   set newText to convertText(oldText, CurrFileCounter)
   
   set eof openFile to 0
   write newText to openFile
   close access openFile
end repeat

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.313 seconds, 9 queries executed ]

RSS (new topics) RSS (active topics)