I'm looking for a way/code to select a folder within a specific folder that contains part of a string from a variable
I have folderA and within folderA are numerous folders. All the folders within folderA start out with a 5 digit number followed by an "_" and then a short description. ie, 12345_Folder Test 1, 54321_Folder Test 2, 99999_Folder Test 3, etc...
tell application "Finder"
set folderA to folder "FolderA" of folder "Desktop" of folder "MacUser" of folder "Users" of startup disk
set jobNum to text returned of (display dialog "Enter 5 Digits Only:" default answer "")
open folderA
set selected_item to (folder whose name contains jobNum in folderA) -- This is what's failing for me.
-- from here on I would like to do other things such as run other parts of a script.
end tell
---Script Error Message
Can’t get folder whose name contains jobNum of folder "FolderA" of
folder "Desktop" of folder "prepress_js" of folder "Users" of startup
disk of application "Finder". ----
Any help is greatly appreciated!
Try this line:
set selection to item 1 of (get every folder in folderA whose name contains jobNum) -- This is what's failing for me.
Related
I'm new to AppleScript and I try to make droplet that recursively changes permissions on dropped folder and files within. I wrote this code and it works fine to all items within the root folder but it can't change permissions on the root folder itself. What should I change in this code?
property user_name : "suser_root"
property pass_word : "**********"
on open theDroppedItems
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
do shell script "sudo chmod -R 777 " & quoted form of POSIX path of theCurrentDroppedItem user name user_name password pass_word with administrator privileges
end repeat
end open
You need HOT FOLDER conception instead of DROPPLET:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property user_name : "suser_root"
property pass_word : "**********"
on adding folder items to this_folder after receiving theDroppedItems
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
-- make sure dropped item is folder
if (class of item theCurrentDroppedItem of application "Finder") as text is "folder" then
-- unlock dropped folder and its entire contents
tell application "Finder" to set entireContents to (entire contents of folder (theCurrentDroppedItem as text)) as alias list
set end of entireContents to theCurrentDroppedItem
repeat with anAlias in entireContents
set theURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of anAlias))
set {theResult, theError} to (theURL's setResourceValue:(false) forKey:(current application's NSURLIsUserImmutableKey) |error|:(reference))
do shell script "chmod -R 777 " & quoted form of POSIX path of anAlias user name user_name password pass_word
end repeat
end if
end repeat
end adding folder items to
Using applescript, I'm trying to create a .patch file in a set of parallel folders for every .recipe file in a target folder and its sub-folders. This also includes creating a parallel folder structure as the script does it's thing. I've got the steps right, but some part of the detailed grammar is wrong and I can't figure out what and how to fix it. Debugging information I've read so far has not helped me, though I'm sure this is a simple matter of incorrect applescript grammar. Here is the code:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on run
set patchFile to (choose file with prompt "Select patch file...")
set assetsFolder to (choose folder with prompt "Select recipe assets folder...")
set dumpFolder to (choose folder with prompt "Select dump folder...")
makePatches(patchFile, assetsFolder, dumpFolder)
end run
on makePatches(patchFile, targetFolder, dumpFolder)
tell application "Finder"
set recipeCount to count (files in folder (targetFolder) whose name ends with ".recipe")
if (recipeCount > 0) then
set recipeFiles to files in folder (targetFolder) whose name ends with ".recipe"
repeat with eachFile in recipeFiles
set recipePatch to duplicate patchFile to folder dumpFolder
set recipePatch's name to eachFile's name & ".patch"
end repeat
end if
set folderCount to (folders in folder (targetFolder) whose visible is true)
if (folderCount > 0) then
set subFolders to folders in folder (targetFolder)
repeat with eachFolder in subFolders
set newName to folder eachFolder's name
set newFolder to make new folder at folder dumpFolder
set newFolder's name to newName
my makePatches(patchFile, eachFolder, newFolder)
end repeat
end if
end tell
return
end makePatches
Note that if I remove the code for iterating through the folders and simply have it create patches for each .recipe file in a folder, then that code works. Or at least, it does as far as I can tell. So the problem should be somewhere at or after folderCount. The recursion is needed, as is the ability to use choose with prompt. Hopefully that isn't a problem. What is the correct grammar for the iteration section?
After semi-blindly adjusting the code and looking at just about every relevant example I could find, I came up with some code that does exactly what is needed. But I don't really understand what was going wrong with the original, so while I'll post the working code I made, I'd still like to hear what was wrong with the original code explained.
on run
set patchFile to (choose file with prompt "Select patch file...")
set assetsFolder to (choose folder with prompt "Select recipe assets folder...")
set dumpFolder to (choose folder with prompt "Select dump folder...")
makePatches(patchFile, assetsFolder, dumpFolder)
end run
on makePatches(patchFile, targetFolder, dumpFolder)
tell application "Finder"
set recipeFiles to files of targetFolder whose name ends with ".recipe"
repeat with eachFile in recipeFiles
set recipePatch to duplicate patchFile to dumpFolder
set recipePatch's name to eachFile's name & ".patch"
end repeat
end tell
tell application "Finder" to set theseSubFolders to folders of targetFolder
repeat with tSubF in theseSubFolders
set newName to tSubF's name
tell application "Finder" to set newFolder to (make new folder at dumpFolder with properties {name:newName})
my makePatches(patchFile, tSubF, newFolder)
end repeat
end makePatches
I have been trying to cobble together a script to take a list of files from a text document and have applescript go through the list line by line and then do something with the file. In this case it is changing the label on the file, but we would use it in other instances to move files, etc.
It works with a test file on my desktop the files get marked with the purple Label, but when trying to run it in the folder I actually need to it fails with this error message:
error "Finder got an error: Can’t set 1 to 5." number -10006 from 1
The text files are the same except for the length of their content.
Could this be a an issues with filenames, and if so how do I make the script more tolerant.
Here is the script:
set textFileContents to POSIX path of (choose file)
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "System Events"
repeat with thisFileName in dataList
tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat
end tell
Any help would be appreciated, thanks.
1080074 3.tif
1080074 2.tif
1080069_A1.tif
Here is the final code from the solution to this problem and some further work I did.
Thanks to #Mark Setchell & #jackjr300 for all of their patient help.
set justpath to POSIX path of (choose folder with prompt "Select the Folder with Files You Want to Use")
set textFileContents to (choose file with prompt "Select the list of files")
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
repeat with FileName in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of (justpath & FileName as POSIX file as alias) to 5
end try
end repeat
end tell
You seem to have a spurious tell application "System Events" in there. It works like this:
set FileName to POSIX path of (choose file)
set FileRecords to paragraphs of (read FileName)
repeat with ThisFileName in FileRecords
say ThisFileName
tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat
Note that my test file isn't UTF8.
Update
By the way, if all you want do is set the label colour on some files, it may be easier to do that from the Terminal and not worry with Applescript. Say you start the Terminal, and go to your Desktop like this
cd Desktop
you can then change the labels of all files on your Desktop (and in any subdirectories) whose names contain "Freddy" followed by "Frog" (i.e "fileForFreddyFrog.txt", "file from Freddy the Frog.php")
find . -name "*Freddy*Frog*" -exec xattr -wx com.apple.FinderInfo "0000000000000000000700000000000000000000000000000000000000000000" {} \;
You need to specify the folder because the finder has no current folder, except the Desktop if you don't specify a folder.
set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
set theFolder to "/Users/jack/Desktop/xxx/yyy" as POSIX file as alias -- change it to the path of your folder
tell application "Finder"
repeat with thisFileName in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of file thisFileName of theFolder to 5
end try
end repeat
end tell
Change the path in the third line of this script
--
If the text file contains the full path of the file, you can use this script.
set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
repeat with thisPath in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of (thisPath as POSIX file as alias) to 5
end try
end repeat
end tell
Is it possible to delete all the subfolders (with content) and files within a folder?
For example:
Backup
November
pic1.jpg
pic2.jpg
December
January
pic3.jpg
example1.txt
example2.txt
example3.txt
There is a root folder (Backup). This root folder contains 3 subfolders (with content) and 3 text files. How can I delete the whole content (3 subfolders and 3 files) of the Backup folder without deleting the root folder (Backup) itself?
The Directory class has a Delete method that accepts a parameter that forces the deleting operation recursively on the folder passed
' Loop over the subdirectories and remove them with their contents
For Each d in Directory.GetDirectories("C:\Backup")
Directory.Delete(d, true)
Next
' Finish removing also the files in the root folder
For Each f In Directory.GetFiles("c:\backup")
File.Delete(f)
Next
FROM MSDN Directory.Delete
Deletes the specified directory and, if indicated, any subdirectories
and files in the directory.
I'm creating an automator service that will mark my invoices as paid... I've got a workflow that gets selected PDF, sets variable to path of PDF, Watermarks it, then returns it to it's original location. However the final step doesn't work as the path variable links to the PDF rather than a folder.
Please help.
You can get the parent folder of your input like this:
on run {input}
tell application "Finder" to set myFolder to (container of (first item of input)) as alias
return myFolder
end run