Split large CSV file into smaller files

(1) There is no reason to encapsulate a call to read in a tell Finder block.

If what you posted is encapsulated in a tell Filemaker block, try to use :

tell me to set leTexte to read file theMainFile from beg for 100000000

(2) If your operating system is compatible with DJ Bazzie Wazzie’s AppleScript Toolbox, try to use its code.
It take benefit of powerful pieces of code so I guess that it’s faster that my old fashioned code.

(3) In your environment (FileMaker) are you allowed to use properties or script objects ?
If you are there is a way to fasten the code related to a list.

I will edit the code assuming that you may use a script object.

Yvan KOENIG (VALLAURIS, France) mercredi 15 juillet 2015 22:16:32

Here is an edited version using a script object




script o
	property enListe : {}
end script

# Define the path to the source CSV file
set theMainFile to (choose file with prompt "Select EOS CSV file..." of type {"csv"}) as text
--set theMainfile to (path to home folder as text)&"Downloads:Up Here.csv"
tell application "System Events"
	set sourceLength to size of file theMainFile
end tell
# Define (and possibly create) the folder where new files will be stored
set p2f to "Mandela:Users:Jake:JDLD:01-Projects:UPH Local:04-HiLight:" as text
--set p2f to path to desktop as text
set storage to "Import"
set targetFolder to p2f & storage & ":" # Don't forget the colon!
tell application "System Events"
	if not (exists folder targetFolder) then
		make new folder at end of folder p2f with properties {name:storage}
	end if
end tell

set beg to 1
repeat # external loop
	# Read a chunk of text.
	# As is, assumes that the longer sublist descriptor is less than 100,000,000 characters long
	--tell application "Finder" # I NEVER ENCAPSULATED READ IN A TELL FINDER BLOCK !
	set leTexte to read file theMainFile from beg for 100000000
	--end tell # I NEVER ENCAPSULATED READ IN A TELL FINDER BLOCK !
	
	# Remove the strings "START_"
	set duTexte to my supprime(leTexte, "START_")
	# Split the chunk of text using "END_" as delimiter
	set o's enListe to my decoupe(duTexte, "END_")
	
	set oldnameOfSublist to ""
	repeat with i from 1 to ((count o's enListe) - 1) # internal loop
		(* I'm quite sure that the code used here may be enhanced but my goal  was to build quickly a script doing the job. It will be time for refinements if it prove to be satisfying. *)
		set item_i to item i of o's enListe
		if item_i contains "," and (last paragraph of item_i does not contain ",") then
			set nameOfSublist to paragraph 1 of item_i
			if nameOfSublist is "" then set item_i to text 2 thru -1 of item_i
			if (paragraph 2 of item_i) does not contain "," then set nameOfSublist to paragraph 2 of item_i
			
			set lenOfBlock_i to (count item_i) + (count ("START_")) + (count ("END_")) + (count nameOfSublist) - (count oldnameOfSublist)
			set oldnameOfSublist to nameOfSublist
			set beg to beg + lenOfBlock_i
			if i = 1 then
				set firstParagraph to 2
			else --  if i < (count o's enListe) then
				set firstParagraph to 3
			end if
			set aSublist to my recolle(paragraphs firstParagraph thru -2 of item_i, return)
			
			set fpath to targetFolder & nameOfSublist & ".csv"
			my writeto(fpath, aSublist, text, false)
		end if
	end repeat # internal loop
	
	set beg to beg + 1
	if beg ≥ sourceLength then exit repeat
end repeat # external loop

#=====

on decoupe(t, d)
	local oTIDs, l
	considering case # ADDED So that it split upon START_ or END_ but not upon start_ or end_
		set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
		set l to text items of t
		set AppleScript's text item delimiters to oTIDs
	end considering # Restore the standard behavior
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(* removes every occurences of d in text t *)
on supprime(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to ""
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end supprime

#=====
(* Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861 *)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		tell application "Finder"
			write theData to openFile starting at eof as dataType
		end tell
		close access openFile
		return true
	on error
		try
			close access targetFile
		end try
		return false
	end try
end writeto

#=====

I made also a small change which may fasten slightly the treatment.

Yvan KOENIG running Yosemite 10.10.4 (VALLAURIS, France) mercredi 15 juillet 2015 22:39:33

I am honestly not sure whether FIleMaker allows properties and script objects.

I have tried your new code but when I try to have FileMaker compile it, I get an error saying “expected end of line, etc. but found class name” and an indication that the problem lies with the word “file” in the “set leTexte to read…” line.

When I try to use “tell me” like you suggested, I get basically the same error on the same word

When I un-comment my Tell Finder lines, it works, but I know you’re saying this is a bad idea.

In fact there is an other instruction which you encapsulated in a tell Finder block when my original cose was not doing that.
It’s in the handler writeto.

If FileMaker is odd enough to be unable to treat read and write without the tell Finder block, may you try to code :

tell application “Finder”
tell me to set leTexte to read file theMainFile from beg for 100000000
end tell

and in the handler :

tell application “Finder”
tell me to write theData to openFile starting at eof as dataType
end tell

Here is why I urge you to do that.

If I run the script :

set theFile to (path to desktop as text) & "permissions.rtf"

tell application "Finder"
	read file theFile
end tell

the events log display :

tell current application
	path to desktop as text
end tell
tell application "Finder"
	read file "SSD 500:Users:yvankoenig:Desktop:permissions.rtf"
end tell
tell current application
	read file "SSD 500:Users:yvankoenig:Desktop:permissions.rtf"
end tell

Yes, the instruction is called twice because the attempt to ask the Finder to execute it fails silently.

I I code the clean way :

set theFile to (path to desktop as text) & "permissions.rtf"

tell application "Finder"
	tell me to read file theFile
end tell

the events log display :

tell current application
	path to desktop as text
	read file "SSD 500:Users:yvankoenig:Desktop:permissions.rtf"
end tell

As you see, here, the Finder is not involved and we don’t have the odd double call.

Of course,

set theFile to (path to desktop as text) & "permissions.rtf"
tell me to read file theFile

or

set theFile to (path to desktop as text) & "permissions.rtf"
read file theFile

behave the same clean way but as FileMaker is an ass it seems that you can’t use these clean syntax.

Please, let me know if the proposed syntax is accepted by the ass and more, if the script object o is also accepted.
Using this script object is a well known tip used to fasten the treatment of lists.

Yvan KOENIG running Yosemite 10.10.4 (VALLAURIS, France) jeudi 16 juillet 2015 11:05:26

FYI, I believe the problem is that FileMaker uses the words read and write in its own terminology, resulting in a clash. Addressing another app used to be the common workaround.

Thanks Shane

Funny to see that Filemaker, which is a 100% Apple subsidiary, conflicts with Apple terminology.

Yvan KOENIG (VALLAURIS, France) jeudi 16 juillet 2015 14:13:57

I totally agree with that statement. Also the solution of bouncing and relying against an error is not the best way to solve things in my opinion. What if the next and higher secured AppleEvent manager doesn’t bounce that easily anymore? Therefore I would look for another way to read files:

do shell script "cat " & quoted form of POSIX path of HFSFilePath

or when it’s in another encoding than UTF-8 like windows latin 1

do shell script "iconv -f CP1252 <" & quoted form of POSIX path of HFSFilePath

Hello

When I commented an instruction with # I NEVER ENCAPSULATED READ IN A TELL FINDER BLOCK !

I meant :# I NEVER ENCAPSULATED READ IN A TELL FINDER BLOCK in the code which I posted here !

As a quite 72 years old guy I’m quite sure that years ago I wrote what make me react now.

In the OP’s case, as he is facing a FileMaker discrepancy I can’t test your proposals.
Maybe the app accept them but maybe it doesn’t so, I’m just waiting to learn if it accepts what appears as a not too bad workaround:
tell application “Finder”
tell me to read.
end tell

I wish to add that I’m surprised by the way AppleScript evolve.
When we were asked for the first time to stop encapsulating call to OSAX in tell application blocks, many instructions where behaving the way I described:

tell application “Finder”
read.
end tell
tell current application
read.
end tell

Sometimes a non-fatal error message was also issued.
Since, after every new version of the OS, I see more and more encapsulated calls to OSAX which no longer issue the non-fatal error and, at least if my memory isn’t fooling me, some encapsulated one behave now the old fashioned one:
tell application “Finder”
call OSAX command
end tell

As if the engineers changed their advice about this feature.
It’s at least the way I understood recent messages posted by Chris Page (Apple Engineer) in AppleScript-Users mailing list. But maybe it’s that I understood what I wanted to understand :wink:

Yvan KOENIG (VALLAURIS, France) jeudi 16 juillet 2015 15:22:06

Given that it’s only a terminology problem with FileMaker, you could also use this sort of thing:

using terms from scripting additions
	read file "Path:to:file"
end using terms from

I’m just not sure how far back that’s supported with scripting additions.

Actually, it might need to be an alias, or a «class furl» created separately:

set theFile to "Path"to:file" as «class furl»
tell application "FileMaker"
	using terms from scripting additions
		read theFile
                -- or:
		read "Path:to:existing file" as alias
	end using terms from
end tell

Just guessing a bit…

Bingo

On an old disk I retrieve a trial version of FileMaker Pro 12.
So I was able to make a test ;

set theFile to (path to desktop as text) & "bytesTIFF.txt"


tell application "FileMaker Pro"
	using terms from scripting additions
		read file theFile
	end using terms from
end tell

works.

tell application "FileMaker Pro"
	set theFile to (path to desktop as text) & "bytesTIFF.txt"
	using terms from scripting additions
		read file theFile
	end using terms from
end tell

works too and, to be as clean as possible it would perhaps be a good idea to code :

tell application "FileMaker Pro"
	using terms from scripting additions
		set theFile to (path to desktop as text) & "bytesTIFF.txt"
		read file theFile
	end using terms from
end tell

or

tell application "FileMaker Pro"
	using terms from scripting additions
		set theFile to (path to desktop as text) & "bytesTIFF.txt" as «class furl»
		read theFile
	end using terms from
end tell

These two late (maybe late two) versions work.

As you may see, I’m not lying when I write that I am pig-headed :wink:

Yvan KOENIG running Yosemite 10.10.4 (VALLAURIS, France) jeudi 16 juillet 2015 15:44:46

Yes! This new version works. Thank you! And I understand the double instructions problem. I appreciate you taking the time to explain.

For reference, my current code is:




script o
	property enListe : {}
end script

# Define the path to the source CSV file
set theMainFile to (choose file with prompt "Select EOS CSV file..." of type {"csv"}) as text
--set theMainfile to (path to home folder as text)&"Downloads:Up Here.csv"
tell application "System Events"
	set sourceLength to size of file theMainFile
end tell
# Define (and possibly create) the folder where new files will be stored
set p2f to "Mandela:Users:Jake:JDLD:01-Projects:UPH Local:04-HiLight:" as text
--set p2f to path to desktop as text
set storage to "Import"
set targetFolder to p2f & storage & ":" # Don't forget the colon!
tell application "System Events"
	if not (exists folder targetFolder) then
		make new folder at end of folder p2f with properties {name:storage}
	end if
end tell

set beg to 1
repeat # external loop
	# Read a chunk of text.
	# As is, assumes that the longer sublist descriptor is less than 100,000,000 characters long
	
	tell application "Finder"
		tell me to set leTexte to read file theMainFile from beg for 100000000
	end tell
	
	# Remove the strings "START_"
	set duTexte to my supprime(leTexte, "START_")
	# Split the chunk of text using "END_" as delimiter
	set o's enListe to my decoupe(duTexte, "END_")
	
	set oldnameOfSublist to ""
	repeat with i from 1 to ((count o's enListe) - 1) # internal loop
		(* I'm quite sure that the code used here may be enhanced but my goal  was to build quickly a script doing the job. It will be time for refinements if it prove to be satisfying. *)
		set item_i to item i of o's enListe
		if item_i contains "," and (last paragraph of item_i does not contain ",") then
			set nameOfSublist to paragraph 1 of item_i
			if nameOfSublist is "" then set item_i to text 2 thru -1 of item_i
			if (paragraph 2 of item_i) does not contain "," then set nameOfSublist to paragraph 2 of item_i
			
			set lenOfBlock_i to (count item_i) + (count ("START_")) + (count ("END_")) + (count nameOfSublist) - (count oldnameOfSublist)
			set oldnameOfSublist to nameOfSublist
			set beg to beg + lenOfBlock_i
			if i = 1 then
				set firstParagraph to 2
			else --  if i < (count o's enListe) then
				set firstParagraph to 3
			end if
			set aSublist to my recolle(paragraphs firstParagraph thru -2 of item_i, return)
			
			set fpath to targetFolder & nameOfSublist & ".csv"
			my writeto(fpath, aSublist, text, false)
		end if
	end repeat # internal loop
	
	set beg to beg + 1
	if beg ≥ sourceLength then exit repeat
end repeat # external loop

#=====

on decoupe(t, d)
	local oTIDs, l
	considering case # ADDED So that it split upon START_ or END_ but not upon start_ or end_
		set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
		set l to text items of t
		set AppleScript's text item delimiters to oTIDs
	end considering # Restore the standard behavior
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(* removes every occurences of d in text t *)
on supprime(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to ""
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end supprime

#=====
(* Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861 *)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		tell application "Finder"
			tell me to write theData to openFile starting at eof as dataType
		end tell
		close access openFile
		return true
	on error
		try
			close access targetFile
		end try
		return false
	end try
end writeto

#=====

Good news.

May you use this edited version based upon a Shane Stanley’s proposal and the tests which I was able to made ?




script o
	property enListe : {}
end script

# Define the path to the source CSV file
set theMainFile to (choose file with prompt "Select EOS CSV file..." of type {"csv"}) as text
--set theMainfile to (path to home folder as text)&"Downloads:Up Here.csv"
tell application "System Events"
	set sourceLength to size of file theMainFile
end tell
# Define (and possibly create) the folder where new files will be stored
set p2f to "Mandela:Users:Jake:JDLD:01-Projects:UPH Local:04-HiLight:" as text
--set p2f to path to desktop as text
set storage to "Import"
set targetFolder to p2f & storage & ":" # Don't forget the colon!
tell application "System Events"
	if not (exists folder targetFolder) then
		make new folder at end of folder p2f with properties {name:storage}
	end if
end tell

set beg to 1
repeat # external loop
	# Read a chunk of text.
	# As is, assumes that the longer sublist descriptor is less than 100,000,000 characters long
	
	using terms from scripting additions # EDITED
		set leTexte to read file theMainFile from beg for 100000000
	end using terms from
	
	# Remove the strings "START_"
	set duTexte to my supprime(leTexte, "START_")
	# Split the chunk of text using "END_" as delimiter
	set o's enListe to my decoupe(duTexte, "END_")
	
	set oldnameOfSublist to ""
	repeat with i from 1 to ((count o's enListe) - 1) # internal loop
		(* I'm quite sure that the code used here may be enhanced but my goal  was to build quickly a script doing the job. It will be time for refinements if it prove to be satisfying. *)
		set item_i to item i of o's enListe
		if item_i contains "," and (last paragraph of item_i does not contain ",") then
			set nameOfSublist to paragraph 1 of item_i
			if nameOfSublist is "" then set item_i to text 2 thru -1 of item_i
			if (paragraph 2 of item_i) does not contain "," then set nameOfSublist to paragraph 2 of item_i
			
			set lenOfBlock_i to (count item_i) + (count ("START_")) + (count ("END_")) + (count nameOfSublist) - (count oldnameOfSublist)
			set oldnameOfSublist to nameOfSublist
			set beg to beg + lenOfBlock_i
			if i = 1 then
				set firstParagraph to 2
			else --  if i < (count o's enListe) then
				set firstParagraph to 3
			end if
			set aSublist to my recolle(paragraphs firstParagraph thru -2 of item_i, return)
			
			set fpath to targetFolder & nameOfSublist & ".csv"
			my writeto(fpath, aSublist, text, false)
		end if
	end repeat # internal loop
	
	set beg to beg + 1
	if beg ≥ sourceLength then exit repeat
end repeat # external loop

#=====

on decoupe(t, d)
	local oTIDs, l
	considering case # ADDED So that it split upon START_ or END_ but not upon start_ or end_
		set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
		set l to text items of t
		set AppleScript's text item delimiters to oTIDs
	end considering # Restore the standard behavior
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(* removes every occurences of d in text t *)
on supprime(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to ""
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end supprime

#=====
(* Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861 *)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		using terms from scripting additions # EDITED
			write theData to openFile starting at eof as dataType
		end using terms from
		close access openFile
		return true
	on error
		try
			close access targetFile
		end try
		return false
	end try
end writeto

#=====

This one doesn’t apply a workaround, it apply standard rules from a to z.

Yvan KOENIG (VALLAURIS, France) jeudi 16 juillet 2015 21:14:09

Hey Jake,

I wrote something similar to this for you on the TextWrangler-Talk list back in December.

This works with your most recently posted file.


-------------------------------------------------------------------------------------------
# Parse CSV file with embedded CSV files.
# Separate embedded files.
# REQUIRES the Satimage.osax { http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html }
-------------------------------------------------------------------------------------------
tell application "Finder"
	set frontFinderWindowDir to insertion location as alias
	set frontFinderWindowPath to frontFinderWindowDir as text
	set myCsvFile to (first file of frontFinderWindowDir whose name ends with ".csv") as alias
end tell

set nameList to find text "^START[[:punct:][:blank:]]*(\\w+)$" in myCsvFile using "\\1" with regexp, all occurrences and string result
set csvList to find text "(?m)^START.+?$[\\r\\n](.+?)[\\r\\n]^END.+?$" in myCsvFile using "\\1" with regexp, all occurrences and string result

if length of csvList = length of nameList then
	
	repeat with n from 1 to (length of csvList)
		writetext (item n of csvList) to file (frontFinderWindowPath & (item n of nameList) & ".csv")
	end repeat
	
else
	error "Mismatched name and content!"
end if
-------------------------------------------------------------------------------------------

To DJ Bazzie Wazzie

I tried to use your code based upon AppleScript ToolBox.

The original version worked flawlessly.

So, I tried to use it in a tell application “FileMaker Pro” block to be in the OP’s environment.

The tested code is :


# Requires AppleScript Toolbox.osax available for free from : https://astoolbox.wordpress.com

tell application "FileMaker Pro"
	using terms from scripting additions
		set sourceFile to (path to home folder as text) & "Downloads:Up Here.csv"
		set p2f to (path to desktop as text) & "Import:"
		
		my saveSubRows("START_LEVELS", "END_LEVELS", sourceFile, p2f & "levels.csv", linefeed) # inserted my at front
		my saveSubRows("START_TARGETS", "END_TARGETS", sourceFile, p2f & "targets.csv", linefeed) # inserted my at front
		(*
		my saveSubRows("START_CHANNELS", "END_CHANNELS", sourceFile, p2f & "channels.csv", linefeed) # inserted my at front
		my saveSubRows("START_FIXTURES", "END_FIXTURES", sourceFile, p2f & "fixtures.csv", linefeed) # inserted my at front
		my saveSubRows("START_SHOWCONTROL", "END_SHOWCONTROL", sourceFile, p2f & "showcontrol.csv", linefeed) # inserted my at front
		*)
	end using terms from
end tell

on saveSubRows(startTag, endTag, sourceFile, targetFile, rowDelimiter)
	set csvData to AST find regex ("" & startTag & rowDelimiter & "(.*)" & rowDelimiter & endTag) in file sourceFile regex group 2 with case sensitivity
	
	try
		set fd to open for access targetFile with write permission
		set eof of fd to 0
		write csvData to fd as «class utf8»
		close access fd
	on error
		close access targetFile
	end try
end saveSubRows

It compiles well but at execution it stops on the saveSubRows command.

Yvan KOENIG running Yosemite 10.10.4 (VALLAURIS, France) vendredi 17 juillet 2015 11:44:20

BINGO, I got it. I inserted my before the calls to the osax commands and it works.

To ccstone and DJ Bazzie Wazzie

As you know, I’m pig headed. So after hitting a kind of wall with DJ Bazzie Wazzie’s proposal, I tested ccstone’s one and I have more luck with it.


-------------------------------------------------------------------------------------------
# Parse CSV file with embedded CSV files.
# Separate embedded files.
# REQUIRES the Satimage.osax { http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html }
-------------------------------------------------------------------------------------------
(*tell application "Finder"
	set frontFinderWindowDir to insertion location as alias
	set frontFinderWindowPath to frontFinderWindowDir as text
	set myCsvFile to (first file of frontFinderWindowDir whose name ends with ".csv") as alias
end tell
*)
tell application "FileMaker Pro"
	using terms from scripting additions
		set frontFinderWindowPath to (path to home folder as text) & "Downloads:"
		set myCsvFile to (frontFinderWindowPath & "Up Here.csv") as alias
		
		set nameList to find text "^START[[:punct:][:blank:]]*(\\w+)$" in myCsvFile using "\\1" with regexp, all occurrences and string result
		set csvList to find text "(?m)^START.+?$[\\r\\n](.+?)[\\r\\n]^END.+?$" in myCsvFile using "\\1" with regexp, all occurrences and string result
		
		if length of csvList = length of nameList then
			
			repeat with n from 1 to (length of csvList)
				writetext (item n of csvList) to file (frontFinderWindowPath & (item n of nameList) & ".csv")
			end repeat
			
		else
			error "Mismatched name and content!"
		end if
	end using terms from
end tell
-------------------------------------------------------------------------------------------

This version worked perfectly.

So, I’m facing a puzzling question :
why if FileMaker able to use a command from Satimage.osax but is unable to use a command from AppleScript Toolbox.osax ?

Must I assume that Satimage pass to the system some info which aren’t passed by AppleScript ToolBox ?

Yvan KOENIG running Yosemite 10.10.4 (VALLAURIS, France) vendredi 17 juillet 2015 12:04:50

BINGO, I got it. I inserted my before the calls to the AppleScript Toolbox commands and it works.

Hey Jake,

Remember this is a PERL script not an AppleScript.

It looks for 1 single .csv data file in the front Finder window.

It runs on the latest provided data file in sub-1-second times.

#! /usr/bin/env perl 
	use v5.010; use strict; use warnings;
#--------------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/07/17 14:00
# dMod: 2015/07/17 16:16
# Appl: Finder, Perl, and application to run Perl.
# Task: Extract embedded csv files from a master csv file.
# Tags: @Perl, @Script, @CSV, @Extract
# Vers: 1.00
#--------------------------------------------------------------------------------------
# Notes:
#
# Written for user jakedeg on MacScripter.net
#--------------------------------------------------------------------------------------

my ( @dataFiles, $dataDir, $FILE, $fileName, $insLocAppleScript, $newFilePath, $outputFH );

$insLocAppleScript = q(read -r -d '' myAppleScript <<'EOF'
	tell application "Finder" to set frontFolderAlias to insertion location as alias
	return POSIX path of frontFolderAlias
EOF
osascript -e "$myAppleScript");

$dataDir = `$insLocAppleScript`;
chomp $dataDir;
@dataFiles = glob( qq("$dataDir"*.csv) ); if ( @dataFiles != 1 ) { die "Problem with number of data files found! $!\n" };
open($FILE, "<", $dataFiles[0]) or die $!;

while (<$FILE>) {

	if ( m!^START_(\w+)$! ) {

		$fileName = $1 . ".csv";
		$newFilePath = $dataDir.$fileName;
		open($outputFH, ">", $newFilePath) or die $!;

	} elsif ( m!^END_(\w+)$! ) {

		close $outputFH;

	} else {

		print $outputFH $_;
	
	}
	
}

close $FILE;

Thanks for testing it Yvan, I have update my code above.