Im trying to create a list from a delimited text file - applescript

set definitionsPath to "/Users/toto/Desktop/Definitions.txt"
set defs to POSIX file definitionsPath
set temp to defs as string
set defs to read file temp using delimiter {"§"}
But it doesn't work. The AppleScript list isn't created
I've tried to fill the text file by using different approaches :
text1§ text2§ text3§
"text1"§"text2"§"text3"
What should I do ?
Thanks.

Put each item in a new line for example
Text1
Text2
Text3
Then read
set definitionsPath to "/Users/toto/Desktop/Definitions.txt"
set defs to paragraphs of (read definitionsPath as «class utf8»)
As almost all text files are UTF8 encoded it's crucial to add as «class utf8», the default text encoding of AppleScript is MacRoman

Related

How to re-format a .txt list in Applescript

I have a file (list.txt) with a list that contains thousands of lines that look like this:
carpet-redandblue
shelf-brown
metaldesk-none
Is there a script I can use to remove everything after the "-", including the "-" as well?
This is what I have so far:
set theFile to "/Users/home/Desktop/list.txt"
if theFile contains "-" then
set eol to "-"
else
set eol to "-"
end if
But doesn't seem to be working.
Do I have to define an output file with a filename and path?
This should do what you need, at least as I understand it. The script reads the text file into a variable. It then breaks the text into paragraphs (or lines) and splits each line at the first dash. It then converts each line back into paragraphs of a text. Finally, it writes the resulting text to a text file. If you prefer to use the resulting text in some other way, it is stored in the prunedText variable.
use scripting additions
(*
set rawText to "carpet-redandblue
shelf-brown
metaldesk-none"
*)
set rawText to read file ((path to desktop as text) & "list.txt")
set AppleScript's text item delimiters to "-"
set paraText to paragraphs of rawText
--> {"carpet-redandblue", "shelf-brown", "metaldesk-none"}
set wordPara to {}
repeat with eachLine in paraText
set end of wordPara to first text item of eachLine
end repeat
--> {"carpet", "shelf", "metaldesk"}
set AppleScript's text item delimiters to linefeed
set prunedText to wordPara as text
(*
"carpet
shelf
metaldesk"
*)
-- optionally
tell application "Finder"
set nl to ((path to desktop as text) & "newList.txt") as «class furl»
end tell
close access (open for access nl)
write prunedText to nl as text
This in my opinion is the easiest solution.
Open Terminal.app
Go to Finder and while holding the ⌘ key, drag and drop the folder containing your "list.txt" file directly into a Terminal window. This will change your current working directory in Terminal to the folder containing your "list.txt" file.
Then paste this following code into that Terminal window and press the Return key...
grep -E -o "^\w*" list.txt > new_list.txt
This will create a new file called "new_list.txt" with the hyphens and everything after them removed from each line of your original list.txt

Merging files in Applescript

I am trying to do below in AppleScript.
Concatenate/Merge all *.xxx files found in a particular folder into one new file
Each file contains a header. Strip header from all but 1st file before merging.
Add a footer text to the merged file.
This sounds relatively simple in other languages but I am a beginner to applescript. Any help to find a direction would be appreciated.
TIA
AnuRV
Try this, you are prompted to choose a base folder and a destination file name.
Important: Use a destination location outside the base folder to avoid the file to be included in the merging process.
I assume that your tsv file type is a typo and you mean csv.
If not, change all occurrences of csv in the script.
The text delimiter is linefeed (0A), if you need return (0D) change the occurrence of linefeed to return.
set baseFolder to choose folder
set destinationFile to choose file name with prompt "Choose destination file name" default name "merged.csv"
tell application "Finder" to set tsvFiles to (files of baseFolder whose name extension is "csv") as alias list
set text item delimiters to linefeed
try
set fileDescriptor to open for access destinationFile with write permission
repeat with i from 1 to (count tsvFiles)
set theFile to item i of tsvFiles
set theText to paragraphs of (read theFile as «class utf8»)
if i = 1 then
write (theText as text) to fileDescriptor as «class utf8»
else
write ((items 2 thru -1 of theText) as text) to fileDescriptor as «class utf8»
end if
end repeat
close access fileDescriptor
on error
try
close destinationFile
end try
end try
set text item delimiters to {""}

"Can't get last text item of alias" Error When Running AppleScript

I have an AppleScript that runs as part of a Hazel routine monitoring a folder. When the script runs, it picks apart the file targeted by the Hazel routine and then attaches the file to an email and addresses the email with information from the name of the file. Unfortunately, it seems there is an error somewhere in the script, but I cannot seem to locate it.
The only semi-useful information from Console is in the title (i.e., "Can't get last text item of alias"). Here is the script:
on hazelProcessFile(theFile)
set theAttachment1 to (POSIX path of theFile)
set FileName to theFile
--remove trailing slash
set SansSlash to quoted form of text 1 through -2 of FileName
set FileName to SansSlash as text
-- remove path from FileName
set AppleScript's text item delimiters to ":"
set SansPath to last text item of FileName
set FileName to SansPath as text
-- remove extension
set AppleScript's text item delimiters to "."
set SansExtension to every text item of FileName
set last text item of SansExtension to ""
set FileName to SansExtension as text
-- parse using —
set AppleScript's text item delimiters to "—"
set clientName to first text item of FileName
set clientEmail to last text item of FileName
tell application "Airmail 2"
activate
set theMessage to make new outgoing message with properties {subject:"New Invoice from ", content:"Please find attached, infra, the current month's invoice. If you have any questions, please feel free to respond to this email. One-time payments may be made using the following secure form on our website: https://. Thank you for your continued business."}
tell theMessage
set sender to "billing#example.com"
make new to recipient at end of to recipients with properties {name:clientName, address:clientEmail}
make new mail attachment with properties {filename:theAttachment1}
compose
end tell
end tell
end hazelProcessFile
The code is commented, so it should be obvious what each section is supposed to do. I would imagine the issue is in the "remove path from FileName" section, as that is the section that has been giving me the most trouble.
theFile is obviously an alias specifier.
text item and text thru – as the name implies – expects plain text
You have to coerce the alias first to text before dealing with text and remove quoted form of, that's only needed in conjunction with do shell script.
--remove trailing slash
set FileName to theFile as text
set SansSlash to text 1 through -2 of FileName
set FileName to SansSlash
but there is no trailing slash in a HFS path
To strip the file name without extension from an alias this is much easier
tell application "System Events" to set {name:Nm, name extension:Ex} to theFile
set baseName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
Edit:
Try this optimized code, the Airmail 2 part is skipped and I don't know if the EM Dash character is treated correctly in case you'll copy and paste the code.
on hazelProcessFile(theFile)
set theAttachment1 to (POSIX path of theFile)
tell application "System Events" to set {name:Nm, name extension:Ex} to theFile
set FileName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
-- parse using —
set {TID, text item delimiters} to {text item delimiters, "—"}
set clientName to text item -2 of FileName
set clientEmail to text item -1 of FileName
set text item delimiters to TID
-- tell application "Airmail 2"
-- ...
end hazelProcessFile

Applescript: reading a text file with utf8 class triggers error

I want to read from a text file paragraph by paragraph and since the content of the file is in German language the file contains special characters and I understood I have to use class utf8 in order to read the character properly into the script.
I face the problem if I use the suggested command
set txt to paragraphs of (read foo for (get eof foo)) as «class utf8»
I get the error
error "Can’t make {\"\tDate:\t10. J√§nner 2006 20:53\", \"\tTags:\tHase, Muffin, Paul\", \"\tLocation:\tM√ºhlgasse, Wiener Neudorf, Lower Austria, Austria\", \"\tWeather:\t-7¬∞ Clear\", \......
If I read the file without the «class utf8» no error occurs.
I use the following code:
set theFile to readFile("/Users/Muffin/Documents/DayOne-Export/DayOne.md")
-- set Shows to read theFile using delimiter return
repeat with nextLine in theFile
<text processing>
end repeat
on readFile(unixPath)
-- prepare text file to read
set foo to (open for access (POSIX file unixPath))
set txt to paragraphs of (read foo for (get eof foo)) as «class utf8»
-- set txt to paragraphs of (read foo) as «class utf8»
close access foo
return txt
end readFile
The text file looks like this:
Date: 10. Jänner 2006 20:53<br>
Tags: Hase, Muffin, Paul<br>
Location: Mühlgasse, Wiener Neudorf, Lower Austria, Austria<br>
Weather: -7° Clear<br>
1st Sign of Paul’s arrival
.... Actually it was a normal morning and as usual I got up at 6 am start preparing the breakfast.
The error occurs directly in the set txt command.
Any idea why I run into errors?
Your brackets are placed incorrectly:
set txt to paragraphs of (read foo for (get eof foo) as «class utf8»)
Otherwise you would try to convert a list into utf8.
BTW
for (get eof foo) is unnecessary.

can't locate my file in subfolder nor read it (applescript)

I want to read text in a list from a textfile "test.txt" that's located in a subfolder called "data". The text is formatted as plain text, every line ends with enter. After all I want to work around that my code could not be saved anymore because I stored too many data like set names to {"Adam", "Adele",... which pumped the sctp over 500kb, by reading huge lists from textfiles.
Which way I try, I end up getting the xyz.app-name in my set folder, I don't know how its better to use read or do it with POSIX, or I get permission trouble when I use container of to filter my filename, nor where to nest the code to open the file.
Can someone help me start?
EDIT
I tried a bit and got a solution that worked for me, reading my text in a list. See below.
Then I came to another problem: One list I could not save in MacOSRoman, so I had to use Unicode UTF-16. But I can't break myfile2 into a list anymore. Can someone help me out with this?
As a new user I could not upload my screenshot, so here is the code:
set List1 to (read file myFile1 using delimiters linefeed) as list
set List2 to (read file myFile2 using delimiters linefeed as Unicode text) as list
Try any of these:
set aData to read file "Mac OS X:Users:j0k:Desktop:text file.txt"
set bData to read POSIX file "/Users/j0k/Desktop/text file.txt"
set cData to read alias "Mac OS X:Users:j0k:Desktop:text file.txt"
set dData to do shell script "cat '/Users/j0k/Desktop/text file.txt'"
An easy way of getting the path to your file is to drag the file into the script editor.
EDIT:
tell application "Finder" to set myFolder to container of (path to me) as text
set myFile to myFolder & "Data:test.txt"
set aData to read file myFile

Resources