Using bash to navigate to a file in Finder - macos

I drag a lot of graphic files from Finder directly into InDesign and Photoshop. I use a vey simple bash script to quickly open the directory containing the file.
cd "/Volumes/Server/Resources/stock1/"
open .
The script opens the correct directory, but I would like to know how to get it to also go to a specified file (e.g., image.eps) and highlight/select it.
The directories I work with contain hundreds of files and have hard-to-look-through names. This would be a huge time-saver.
Thanks so much for any help. I'm using Mac OSX 10.9.5.

Use the -R (aka --reveal) option to select a single file:
open -R "/Volumes/Server/Resources/stock1/image.eps"
Something like,
open -R "/Volumes/Server/Resources/stock1/"*.eps
will not select all eps files in the folder, but instead will select each one successively, so that the end result is only the last file is selected.

#chepner's answer (-R option) is great if you want to highlight just one file. If you want to select multiple files, you may want to use Apple Script like this:
osascript -e 'tell application "Finder" to select files in folder "stock1" of folder "PHOTOS and IMAGES" of disk "Server" whose name ends with ".eps"'

Related

How to get name of folder for Shell Script in Automator?

I'm using the newest version of macOS Monterey.
Sometimes I have to merge/combine all files that are in a specific folder into one txt file.
I currently do that by typing this in Terminal:
cd /Users/my_name/Desktop/test_folder ; cat * >merged.txt
This will merge/combine all files in folder test_folder into one file called merged.txt. The file merged.txt will get saved into the folder test_folder.
Every time I need this I have to open Terminal copy/paste the command and replace test_folder with the right folder name, since it's not always the same.
I want to make this easier by just make a right click on a folder, go to Quick Actions and select e.g. Merge all files to merge/combine all files inside the folder I just clicked on.
But I stuck at getting the folder name. How can I dynamically get the folder name and path I clicked on to start this Quick Action instead of the hard coded /Users/my_name/Desktop/test_folder?
Or, is there another and easier solution?
This is what I have so far:
I wouldn’t do this with AppleScript, especially if all it’s ultimately doing is calling out to a shell script.
Stick with the Run Shell Script action except change the option for passing the input as arguments rather than to stdin.
The folders selected in Finder will then be available to your script via $#, so you can do something like:
for d in "$#"; do
cat "$d"/* > "$d/merged.txt"
open -R "$d/merged.txt"
done 2>/dev/null
This loops through the selected directories and concatenates the files to merged.txt in the respective directory. The open -R line reveals the merged.txt file in Finder.
Errors are written to /dev/null, i.e. discarded, as cat will throw an error if any of the directories, themselves, contain directories.
Instead of adding a Run Shell Script to your workflow, try adding a Run AppleScript command instead. Copy this following AppleScript code to the Run AppleScript command.
on run {input, parameters}
try
do shell script "cd " & quoted form of POSIX path of input & " && cat *.txt > merged.txt"
on error
try
do shell script "cd " & quoted form of POSIX path of input & " && rm merged.txt"
end try
end try
end run

Applescript to move incoming pictures to other folder with delay

I'm an applescript noob. So I don't know anything about this type of coding.
I want to make a script for a watch folder. I work with an image program that processes images. The process takes a second to create the jpg. So the folder has to wait for a few seconds to move the image to another file.
So this is what I'm looking for: Empty folder - jpg in the folder - wait 2 sec - move jpg to another folder - empty folder.
Thank you!
This AppleScript will accomplish what you asked. By the very nature of AppleScript, reading it should be very self explanatory as to what each line does:
on adding folder items to ThisFolder after receiving SomeFiles
set ThatFolder to POSIX file "/path/to/new/folder"
delay 2
repeat with TheFile in SomeFiles
tell application "Finder" to ¬
if name extension of (TheFile as alias) is in {"JPG", "JPEG"} then ¬
move TheFile to ThatFolder
end repeat
end adding folder items to
This is designed to run as a folder action, which makes the folder you choose automatically monitored by MacOS so that, whenever it detects a change to that folder, it executes the script attached to it. In this instance—as you can infer from the first line of the script—this will automatically run each time a file is added to the folder in question.
To set up a folder action:
Copy the AppleScript above into Script Editor. Edit the second line to replace "/path/to/new/folder" with the path to the new folder into which you want your jpegs to be moved (keep the quotes). I wouldn't bother trying to run the script from inside the editor—it won't work. The path you type out must be in full, i.e. "/Users/Richard/Pictures/Processed" and not "~/Pictures/Processed".
Save it as whatever you like. However, it must be saved in the following directory: ~/Library/Scripts/Folder Action Scripts where ~ indicates your Home folder (i.e. /Users/Richard/ or whatever it is). If the folder "Folder Action Scripts" doesn't exist, create it.
Close Script Editor. Navigate to the folder that is going to be watched, i.e. the folder that your images will be waiting initially. Now navigate one level up, into the directory containing said folder.
Right-click on the folder and hover over the Services menu item at the bottom. Then select Folder Actions Setup...
Enable folder actions by checking the box at the top. If you folder doesn't already appear in the left-hand list, you can add it. Then, in the right-hand list, click the '+' at the bottom and the script you just saved should be one of many in the list of scripts that pop up. Select it and add it.
Make sure the check boxes next to your watched folder and your chosen script are both checked, and you're done.
Now, whenever the folder receives any files whatsoever, that script is executed. The script will move any files with extensions .jpg or .jpeg into your new folder. Just make sure the new folder already exists (I didn't incorporate a line to create it if it doesn't; the script will just throw an error and your image won't be moved).
Here is a little sample script which you could save on your Desktop as monitor:
#!/bin/bash
# Source directory to watch and destination directory to copy to
SRC="$HOME/Desktop/source"
DST="$HOME/Desktop/dest"
# Create directories if not existent
mkdir -p "$SRC" "$DST"
while : ; do
find "$SRC" -type f -iname "*.jpg" -Btime +2s -exec mv {} "$DST" \;
sleep 5
done
It basically watches a directory called Desktop/source and looks for any files in there whose name ends in "JPG" and which have not been modified in the last 2 seconds. If it finds any, it then moves them to directory called Desktop/dest and sleeps for 5 seconds before checking again.
You would need to start Terminal and make the script executable by typing:
chmod +x $HOME/Desktop/monitor
Then, whenever you want it running, you just double-click on the icon of monitor on your Desktop.

Where do I find out when a pycharm python file was made?

If there is no way to find out is there any way to find out when a file was first created on a Mac?
Mac shows the created time when you select a file in Finder. Almost always editors depend on os provided attributes for this.
There are at least two ways to determine this, and for any file in OSX.
The first option works if you are familiar with Terminal and navigation in Unix (Bash etc on OSX). Use the list files "ls" command.
Navigate to the folder your pycharm python file is contained in. Use the ls command to list the contents of that folder (directory) and include the options t,r and U.
For example:
ls -alhtrU
This instructs the ls command to list:
"a" both visible and invisible files in the directory,
"l" in long (single column down the page) format,
"h" with human readable file sizes,
"t" listed in order of time modified/last accessed/created,
"r" in reverse so most recently created file is at the bottom of the list and hence visible if the list is long as nearest to your command prompt. Finally add the
"U" directing the ls command to use the date the file was
created as the time information for ordering and displaying the
files.
This method is not perfect. If the file was created last calendar year, only the year is displayed. If the file was created this calendar year, the created date and time to the minute is displayed. If you include an r in the ls command as suggested, the most recently created files appear in the ls list at the bottom (reverse order). This is helpful if there are many files in that folder/directory and your files of interest were created recently compared to the other files in that directory.
There is likely a different unix command to show the creation date and time of the particular file your interested in.
Learning the options available for basic Unix commands can be very helpful. This and other options for the ls command can be found by entering in Terminal.
man ls
This gives you the manual page for the ls command. Press "q" to exit when your finished reading to return to the Terminal command line. Or open a second Terminal window to load man pages so that you can reference your options in one terminal window while practicing them in the command line in another.
The second option is to open the folder the file your interested in sits in, in the OSX GUI.
Open the folder, then go to the Finder Menu, under View, select View Options. You can tick the box to show file "Date Created".
This solution saves you the time required to learn more about the ls Unix command and has the benefit of a real time update as you create new files in that folder, which may be desirable. However, as downside, if your interested in invisible files (begin with a "." as shown in ls command in Terminal), then these will not be visible without additional OSX tweaks. An alternative here is using Finder, Find, for that folder specifically and using the more detailed options available in Find.

How to create a batch file in Mac?

I need to find a solution at work to backup specific folders daily, hopefully to a RAR or ZIP file.
If it was on PC, I would have done it already. But I don't have any idea to how to approach it on a Mac.
What I basically want to achieve is an automated task, that can be run with an executable, that does:
compress a specific directory (/Volumes/Audio/Shoko) to a rar or zip file.
(in the zip file exclude all *.wav files in all sub Directories and a directory names "Videos").
move It to a network share (/Volumes/Post Shared/Backup From Sound).
(or compress directly to this folder).
automate the file name of the Zip file with dynamic date and time (so no duplicate file names).
Shutdown Mac when finished.
I want to say again, I don't usually use Mac, so things like what kind of file to open for the script, and stuff like that is not trivial for me, yet.
I have tried to put Mark's bash lines (from the first answer, below) in a txt file and executed it, but it had errors and didn't work.
I also tried to use Automator, but it's too plain, no advanced options.
How can I accomplish this?
I would love a working example :)
Thank You,
Dave
You can just make a bash script that does the backup and then you can either double-click it or run it on a schedule. I don't know your paths and/or tools of choice, but some thing along these lines:
#!/bin/bash
FILENAME=`date +"/Volumes/path/to/network/share/Backup/%Y-%m-%d.tgz"`
cd /directory/to/backup || exit 1
tar -cvz "$FILENAME" .
You can save that on your Desktop as backup and then go in Terminal and type:
chmod +x ~/Desktop/backup
to make it executable. Then you can just double click on it - obviously after changing the paths to reflect what you want to backup and where to.
Also, you may prefer to use some other tools - such as rsync but the method is the same.

How to create a txt with file names and save it to same folder with Automator on OSX?

I want to make a .txt file inside the same folder that contains all the filenames of said folder.
How can i do that?
Thanks.
you can do this in terminal by using
ls {DIR} >> {FILE}
for example if i wanted to write a file called 'ls.txt' of all the files on the desktop i could use
ls /Users/corvinmcpherson/Desktop/ >> /Users/corvinmcpherson/Desktop/ls.txt
you can also create one using automator like this:
Or you can create an Automator Workflow like this as a Service, so that all you have to do is right click the folder, click the service and this text file will appear in the folder, with the name of the folder.

Resources