Select all folders with Automator - applescript

How can I create a custom Automator command to simply select all folders within a folder?
If I runned the action it would look like this.
Thanks!
Nicolas

Add:
Get Folder Contents (this will get all items in the given input folder)
Filter Finder Items
and set Kind is Folder
For testing add Reveal Finder Items at the end. Screenshot:
The result will be the list of folders in the given input folder. This is simple but slow solution. (The Get Folder Contents action is very slow.
The fast solution could be written using some scripting. Actions:
Run Shell Script (set the Pass Input: as arguments) and add the following script:
for f in "$#"
do
find "$f" -maxdepth 1 -mindepth 1 -type d -print
done
The result is as above, just much-much faster.

Here is an AppleScript you can add to a "Run AppleScript action" In Automator
set theFolder to (choose folder)
tell application "Finder" to select every folder of theFolder

Related

Automator / Applescript : how to get original from folder alias

I'm trying to create a context menu shortcut to open a file/folder in VS Code from the original item or its alias
So far I was able to create an Automator Service, which:
receives selected: files or folders
in: any application run
shell script:
open -n -b "com.microsoft.VSCode" --args "$*"
How can I change it to accept also aliases?
Symbolic links should be OK, but Finder aliases usually don't work, since most shell utilities see them as small data files and don't know how to interpret them. One solution would be to add a Run AppleScript action to look for aliases in the input and use the original item instead, for example:
Service receives selected files or folders in any application
Run AppleScript:
on run {input, parameters}
set output to {} -- this will be a list of the output items
tell application "Finder" to repeat with anItem in the input
if anItem's kind is "Alias" then
set the end of output to POSIX path of (original item of anItem as alias)
else
set the end of output to POSIX path of anItem
end if
end repeat
return output
end run
Run Shell Script, etc

Applescript to catalog Filename and Filepath

I need track files being sent out to various vendors. I store them in a Filemaker database, but have been trying to develop more efficient ways to fill my database. I created an automator app to create this list for me, but since the "Filter Finder items" relies on Spotlight, it doesn't work on networked drives.
mcgrailm made a slick Applescript that helps me populate a list of the files themselves. This is sufficient for about 90% of my needs as usually I just need to keep a record that a file has been sent, but sometimes I need to also track the POSIX filepath to tell a recipient where on the drive the files are located.
mcgrailm's script was
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
Does anybody know how I can modify this script to give me the full POSIX filepath along with the filename? I tried modifying but my Applescript skills are pretty lacking. Thank you
entire contents in Finder could be very slow. A much faster solution is
set theFolder to POSIX path of (choose folder with prompt "Please select directory.")
set file_list to do shell script "find " & quoted form of theFolder & " -type f ! -name '.*'"
the result is plain text. One full POSIX path per line.
If you want a list use
set file_list to paragraphs of (do shell script "find " & quoted form of theFolder & " -type f ! -name '.*'")

AppleScript Zip contents of folders without including any folders

I am trying to write an apple script app widget that will do the following.
When you drag multiple folders onto the widget it will zip the contents of each folder as a separate zip, these zips will be in the same directory as the widget.
The problem I am having is that I cannot work out how to remove the initial folder, when you unzip the archive, it needs to have all the files and subFolders loose.
For example if I zip myFolder/index.html
And myFolder/img/myImage.png
It should unzip as just index.html and img/my image.png
This would be incredibly useful to me, I have to zip the contents of folders about 50 times a day, renaming the zips with the name of the folder and copying them into the parent folder.
If someone could help with this I'd be willing to offer a mystery prize, I have a ton of steam keys lying about!
Thanks
Will
This should help you get started. If you don't understand a behaviour then feel free to ask.
on open theFiles
repeat with x in theFiles
set _path to POSIX path of x
tell application "Finder"
if kind of x is "Folder" then
tell me to zipFolder(_path)
end if
end tell
end repeat
end open
on run
-- Handle the case where the script is launched without any dropped files
open (choose folder with multiple selections allowed)
end run
on zipFolder(theFolderPath)
do shell script "cd " & quoted form of theFolderPath & " && zip -r \"../$(basename $(pwd)).zip\" ./"
end zipFolder
Here is the full applet Also you might wanna look into apps like LaunchBar, Alfred, and Keyboard Maestro, they help creating actions like this. There a are apps for archives such as Archiver, and Entropy which come with actions such as this.
If you wanna learn applescript, then here are a few features you could consider adding.
Make applet ask for a name for the archive, and use that.
display dialog handler, and pass results to the zipFolder handler as second argument
Reveal the file in Finder after creating it.
tell Finder to reveal
Move archive to a specific directory to upload it e.g. Dropbox
tell Finder to move/copy, or bash command mv
Make applet handle files too. If whatever behaviour you want it to.
Others can only help you so much because they might not fully understand dilemma you are facing, so learning this would be quiet useful, and fun.
For anyone interested this is the final script I am using. This script takes any folders selected and creates a zip archive from each of them, renames them to the folder name and does not include the root folder within the zip. This is especially useful for making Flashtalking HTML5 banners.
on run {input, parameters}
if input is {} then -- no dropped items
tell application "Finder" to set input to selection as alias list
end if
repeat with x in input
set _path to POSIX path of x
tell application "Finder"
if kind of x is "Folder" then tell me to zipFolder(_path)
end tell
end repeat
end run
on zipFolder(theFolderPath)
do shell script "tDir=" & (quoted form of theFolderPath) & "; cd \"$tDir\"; aZip=\"../$(basename \"$tDir\").zip\"; if [ -e \"$aZip\" ]; then rm \"$aZip\"; fi; zip -r \"$aZip\" ./"
end zipFolder

Run several Applescripts from within current script

I'd like to expand my existing...
run script file "Macintosh HD:Users:pathTo:myScript.scpt"
to run all scripts found in a given directory. I've tried...
tell application "Finder" to set scriptsToRun to files of folder POSIX file "/Users/pathTo/" as alias list
run script file scriptsToRun
but no luck with that. Also unless necessary I don't particularly need to involve Finder in my equation. Any suggestions appreciated.
scriptsToRun is a list, so you need to repeat over the list and run each one separately. Notice I used parenthesis to ensure the code is interpreted correctly in the Finder line.
Also notice you don't need "file" in the "run script" line because the list of files is already a list of alias files... from the Finder line. You would only need the word "file" if you had a list of files in string format, then you'd use "file" before each string to make it a file specifier before running it.
Good luck.
tell application "Finder" to set scriptsToRun to (files of folder POSIX file "/Users/pathTo/") as alias list
repeat with aScript in scriptsToRun
run script aScript
end repeat

Apple Script - Selecting files, exclude type, zip it

I need to zip the content of a folder (and all the subfolders) for hundreds of folders.
Is it possible to run a command that takes all the files of a specific folder (prompt), except all the files that have a .fla extension and zip this content into one zipfile?
Right now I am copying the folder, search for all the .fla-files, then select all the files inside the folder (I have the to zip the content, not the folder) and create a zip of it (takes way too long.
I know that it is possible to use Apple Script to delete and copy files. But does this also work in the above mentioned order + zipping?
Alright, so I was still kind of stuck with this issue.
I created a Bash Script, that is executed via an Applescript executable File that has only one line of code:
do shell script "/Volumes/Work/createZipFile.sh"
The Bash Script opens Applescript which lets me prompt a folder (I know, kind of silly to open AS to run a Bash Script that runs AS). The variable is then used to zip this folders content without the .fla files.
myFolder=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfolder to choose folder with prompt "Select the Folder that you want to zip!"
end tell
return (posix path of myfolder)
EOT`
cd $myFolder
zip -r ZipMe.zip . -x ".fla"
echo "A zip File has been created"
So this script does actually work for some folder I try to zip.
But unfortunately not for every folder I chose. Sometimes (no idea why) it seems like it can not find the folder I chose with the prompt, so I starts (at least the zip-process starts running like crazy and doesn't stop) zipping my whole drive.
Any ideas what could be wrong?
In case anybody wants to use this script (which I highly doubt ;)), here is my final version of it.
#!/bin/bash
#Opens an applescript prompt window to select a folder
myFolder=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfolder to choose folder with prompt "Select the Folder that you want to Zip!"
end tell
return (posix path of myfolder)
EOT`
# Terminate if the path is empty (canceled)
if [ -z "$myFolder" ];
then
#echo "Chose a folder!"
exit 0
else
#Change the directory to the above selected folder
cd "$myFolder"
# Creates a ZipFile with todays date of the selected folder, neglecting the after -x listed filetypes
zip -r ZipFile_`eval date +%Y_%m_%d`.zip . -x "*.fla*" "*.AppleDouble*" "*.DS_Store*"
#echo "A zip File has been created"
fi
Your first step should be to figure out what "Kind" of file the .fla is. To do this, run this script and select one of your .fla files:
tell application "Finder"
set theFile to choose file
display dialog (kind of theFile) as string
end tell
And then to get all of the files BUT that type in any folder, you can run this script (Replacing "Plain Text" with whatever type your .fla's turn out to be):
tell application "Finder"
set thePath to choose folder
set theFiles to get every file of folder thePath whose kind is not equal to "Plain Text"
end tell
from there it's just a matter of zipping. After doing some quick googling it looks like the easiest way to zip from applescript is by using do shell script, which shouldn't be that bad now that you have all the files you need in a nifty little array. If you're going for speed though, I might suggest moving this whole project over to bash. That should also simplify things quite a bit. Best of luck!

Resources