can't locate my file in subfolder nor read it (applescript) - 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

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

copy a file with applescript results in "Access Not Allowed"

I am trying to copy a file over another file using applescript.
tell application "Finder"
copy file "Macintosh HD:Users:rkohr:Dropbox:Hacks:hosts.block" to "Macintosh HD:Users:rkohr:Dropbox:Hacks:hosts"
end tell
Both files have be set with chmod 664, and should have permissions to be written to, but it gives a syntax error saying "Access Not Allowed"
First, 'copy' —as a command— doesn't mean 'copy a file' in applescript. The command to copy a file with the Finder is 'duplicate'. Second, what goes between the quotes is just a text string until it is prefaced with 'file' or 'alias' or some such keyword. So your second reference (i.e. the destination) needs to incorporate that. As is, you're trying to have the script copy a file to a string. Finally, renaming the resulting file is a separate action.
Try this:
tell application "Finder"
set sFil to file "rkohr:Dropbox:Hacks:hosts.block"
set fNam to name of sFil
set AppleScript's text item delimiters to ".block"
set sNam to first text item of fNam
set dFil to duplicate sFil to "rkohr:Dropbox:Hacks:" with exact copy
set name of dFil to sNam
end tell
NB If you have a variety of file extensions, then you can get the 'name extension' of your source file and use that as the delimiter. The 'exact copy' parameter will include the file's permissions in the copy. There is also the option of replacing an already existing destination file. I'm assuming that your file path is functional but having it reach back to the disk (e.g. prepend with "MacintoshHD:Users:" or whatever your setup is) might be worth considering.

How can I find files by content in Mac OS X?

I want to find files at a given location whose content matches a given string. For example, there are a lot of files inside the desktop folder (or anywhere), like *.pdf, *.rtf, *.doc, *.txt, *.html and so on.
The user will be prompted to enter a string thistext and select the location /Users/UserName/Desktop. I want to get a list of the files from this location whose content contains thistext.
I found a command utility mdfind, but it returns the files whose name contains thistext as well. I don't want these files in the result list; I only want files whose content is thistext. I've used grep, but it's not working properly for me. Is there a way to customize grep or mdfind command to work for me?
Or if there is any AppleScript script available for performing such task?
I think there are some syntax errors in the above answer.
I just tested this in AppleScript, and it works for me in Yosemite 10.10.5:
set textToSearchFor to "YourTextHere"
set searchDir to "~/Documents/Test/"
set cmdStr to "mdfind 'kMDItemTextContent == \"*" & textToSearchFor & "*\"cd' -onlyin " & searchDir
set lstFiles to (do shell script cmdStr)
log lstFiles
Result:
(*/Users/UserName/Documents/Test/PDF_Log.txt*)
You can specify a query that only examines each file's text content, like so:
mdfind -onlyin ~/Desktop 'kMDItemTextContent == *thistext* cdw'
The cdw at the end of the query string means the comparison should ignore case, diacritics, and width (which is mostly relevant for text with Asian characters).
Also, if you're doing this from an app, you shouldn't invoke the mdfind command as a subprocess. You should use the NSMetadataQuery class to do it within your app.

AppleScript to read text file containing file names, search for each file name, and if a file is found copy it to a folder

Sorry to ask a question without even pasting my coding attempt, but I've never used AppleScript before and I have no idea how I would do this. I've found bits of code online that do small parts of each step of this, but some of the key parts I can't figure out how to do. If I can get this figured out it would save a lot of time. Basically my problem is that a client sent over thousands of photos, all in multiple levels of sub folders, along with an Excel document containing about 300 file names that I need to pull out and use. I can copy the file names from the Excel document into a plain text file, either multi-line or comma separated.
So this is what I need to do:
Open folder selector dialog to select the destination folder
Open file selector dialog to select the text file
Loop through each line (or comma separated value) of the text file
Take that string and search for a file name containing the string
Copy the first result into a folder (let's say Desktop:Found Photos)
If a file could not be found matching the string then add the search string into a text file (so I could email it to the client and ask them to send it to me)
If you can't code this whole process, if you could help me with looping through the text file, searching for the file name and copying the first result to another folder, and adding the file name to a text file if a file wasn't found, then I could probably piece it it all together. Thanks for any help.
You can try something along the lines of:
set newFolder to POSIX path of (path to desktop as text) & "Found Photos"
do shell script "mkdir -p " & quoted form of newFolder
set filePaths to paragraphs of (read (choose file with prompt "Select file list") as «class utf8»)
set fileFolder to POSIX path of (choose folder with prompt "Select folder containing files")
set foundFiles to {}
repeat with fileName in filePaths
set fileName to (contents of fileName)
set xxx to do shell script "find " & quoted form of fileFolder & " -name " & quoted form of fileName
if xxx ≠ "" then
tell application "System Events" to move file xxx to newFolder
set end of foundFiles to fileName & return
end if
end repeat
set foundFiles to (foundFiles as text)
do shell script "echo " & quoted form of foundFiles & " > " & quoted form of POSIX path of ((path to desktop as text) & "FoundFiles.txt")
It might have been easier to use shell scripting:
IFS=$'\n'
mkdir -p ~/Desktop/target/
for l in $(cat ~/Desktop/files.txt); do
found=$(find ~/Documents/source -type f -name "*$l*")
[[ -n $found ]] && cp $found ~/Desktop/target/ || echo "$l"
done

Updating script to find a new type of file

I need to update the script shown below to find a new file type and naming system and have no idea what I'm doing. It use to pull a file named DQXXXXX1.eps and placed in the listed location. The new files are XXXXX_random.pdf. The random in the file name is several series of numbers that change for each file. The important numbers are the first 5, I would like the script to pull all files in that initial location and place into the other location.
The current script is:
set DQfolder to alias "Prepress:ArtFiles:00-Logos A to Z:1-DQ:"
tell application "Finder"
display dialog "enter number" default answer ""
set theNum to text returned of result as string
--try
move alias (DQfolder & "DQ" & theNum & ".eps" as string) to "Macintosh HD:__DQ Incoming:" with replacing
--on error {}
--move alias (DQfolder & "DQ" & theNum & ".tif") to "Macintosh HD:__DQ Incoming:" with replacing
--end try
end tell
Try your script like the following. You have a few things that need to be fixed...
Basically you are adding the strings wrong. You need to first coerce DQfolder to a string before you can add the other strings to it. The way you are doing it can't work because DQfolder is an alias so you can't add other strings to it until you coerce the alias to a string.
the folder path in your command has to be an alias-type file or a folder specification. As such you need to put the word "alias" or "folder" in front of it. A string path will not work. Applescript rarely works with string paths.
"display dialog" is not a Finder command and as such you don't need to put it in the Finder block of code.
"text returned" from your display dialog command is already "text" so you do not need to coerce it to a string. That's why it's called "text" returned... even if you entered a number it's class is still text.
Of course I can't check this code because I don't have files at those paths, but it should work. Good luck...
set DQfolder to alias "Prepress:ArtFiles:00-Logos A to Z:1-DQ:"
display dialog "enter number" default answer ""
set theNum to text returned of result
tell application "Finder"
move alias ((DQfolder as text) & "DQ" & theNum & ".eps") to alias "Macintosh HD:__DQ Incoming:" with replacing
end tell

Resources