Applescript exclude file types on zip - macos

I need to adapt an applescript I have to exclude several file types. I think I need something like -x ".fla" but I can't seem to get the syntax working.
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
Thank you for any help you can give

Add the exclude option before the last double quote:
./ -x *.fla"

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 copy files based on partial file name

Forgive me, I'm a bit of an amature.
I'm working to comply with a records request for hundreds of students. All files are named with the first 5 digits of the name being the student's ID number. I created the script below and it runs but with no results.
I would welcome any help that you guys can provide.
with timeout of 3600 seconds
tell application "Finder"
set myFiles to files of folder POSIX file "/Volumes/Storage/Records" as alias list
end tell
repeat with aFile in myFiles
tell application "System Events"
set myvalues to {"11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999", "00000", "11112", "22223", "33334", "44445", "55556", "66667", "77778", "88889", "99990"}
if name of aFile contains myvalues then
copy aFile to folder POSIX file "/Volumes/Storage/Records"
end if
end tell
end repeat
end timeout
First of all, the command to copy files in terminology of Finder and System Events is duplicate.
Second of all (and the main issue) you have to check the first 5 characters not the entire name.
And third of all, it’s not quite useful to copy the files to the same place but I guess it’s only a placeholder
property IDNumbers : {"11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999", "00000", "11112", "22223", "33334", "44445", "55556", "66667", "77778", "88889", "99990"}
with timeout of 3600 seconds
tell application "Finder"
set myFiles to files of folder "Storage:Records:"
repeat with aFile in myFiles
set IDPrefix to text 1 thru 5 of (get name of aFile)
if IDPrefix is in IDNumbers then
duplicate aFile to folder "Storage:Records:Destination:"
end if
end repeat
end tell
end timeout
My solution uses only the Finder because it doesn't include the invisible files and it's not needed to coerce the Finder object specifiers to alias.
If the location of the files is on an external volume, it’s easier to use the HFS path “Storage:Records” than the coercion from POSIX path POSIX file "/Volumes/Storage/Records” as alias
I suggest you to use do shell script function instead of using Finder application as it's more flexible solution. And you can find any errors in your command easily because you can debug it using the Terminal.
do shell script "<your commands here>"
For your use case you need to use a couple of commands inside do shell script: find and then cp.
I would suggest a simple bash script is a better option here. Save the following script on your Desktop as CopyFiles
#!/bin/bash
# Make a subdirectory to copy the results to
mkdir results 2>/dev/null
# Read all ids from file "ids.txt"
while read id; do
echo Processing id: $id
# Remove the word "echo" on following line to actually copy files
echo cp /Volumes/Storage/Records/${id}* results
done < ids.txt
It assumes there is a file on your Desktop called ids.txt that looks like this
11111
22222
12345
54321
Then start a Terminal, by pressing Cmd+Spacebar and typing "Terminal" and hitting "Enter".
Go to your Desktop and make the script executable with
cd Desktop
chmod +x CopyFiles
and then run it with
./CopyFiles
At the moment, it does nothing except tell you what it would do like this:
Processing id: 11111
cp /Volumes/Storage/Records/11111* results
Processing id: 22222
cp /Volumes/Storage/Records/22222* results
Processing id: 12345
cp /Volumes/Storage/Records/12345* results
Processing id: 54321
cp /Volumes/Storage/Records/54321* results
If it looks like it is doing what you want, edit the script and remove the word echo where noted and run it again.

Applescript administrator privledges but *not* running a shell script

I'm trying to get my Applescript code to have administrator privileges. However, the only solution I find by googling is:
do shell script "command" user name "me" password "mypassword" with administrator privileges
I'm not running a shell command.. I'm using pure Applescript. My code I'm doing is:
on run {input, parameters} -- copy
repeat with aFile in input
tell application "Finder"
if name extension of aFile is "component" then
copy aFile to "/Library/Audio/Plug-ins/Components"
else if name extension of aFile is "vst" then
copy aFile to "/Library/Audio/Plug-ins/VST"
end if
end tell
end repeat
end run
Is there anyway to get admin privileges while using pure Applescript?
Your handler starts with on run {input, parameters} so I think we are talking about a execute applescript step inside an Automator workflow. At this point I think the Automator action is always executed inside the current user's context.
BUT: Of course you can use the do shell script inside the executed Applescript action and at that moment you can give the administrator privileges to that do shell call. I've rebuilt your handler in the following way:
on run {input, parameters} -- copy
-- collect all resulting cp statements in a list for later use
set cpCalls to {}
-- walk through the files
repeat with aFile in input
tell application "System Events"
-- get the file extension
set fileExt to name extension of aFile
-- get the posix path of the file
set posixFilePath to POSIX path of aFile
end tell
if fileExt is "component" then
-- adding the components cp statement to the end of the list
set end of cpCalls to "cp " & quoted form of posixFilePath & " /Library/Audio/Plug-ins/Components/"
else if fileExt is "vst" then
-- adding the vat cp statement to the end of the list
set end of cpCalls to "cp " & quoted form of posixFilePath & " /Library/Audio/Plug-ins/VST/"
end if
end repeat
-- check if there were files to copy
if cpCalls ≠ {} then
-- combine all cp statements with "; " between
set AppleScript's text item delimiters to "; "
set allCpCallsInOne to cpCalls as text
set AppleScript's text item delimiters to ""
-- execute all cp statements in one shell script call
do shell script allCpCallsInOne with administrator privileges
end if
end run
The action now asks for the administrator credentials but you can add user name "me" password "my password" if you like.
To avoid prompting for credentials for each cp I collect all calls in a list and execute them at once at the end of the handler.
Greetings, Michael / Hamburg

How to pass file path to AppleScript in Automator?

I want to be able to right click on a file or folder in Finder and select Services > Open Terminal to open a terminal window at that path.
In automator, I have a service that runs an AppleScript
tell application "Terminal"
do script "cd $filePath"
activate
end tell
I don't know how to pass the file path!
Bonus: How can I ensure that this works for both files and folders? If its a file, it may say that the file is not a directory.
Bonus: Where could I have found this answer myself? The documentation seems to be way to dense.
Thanks
Chet
Note the "Service receives selected ... " at top, which gives result to applescript.
This will open folders and containers of files, but won't be redundant.
on run {input, parameters}
set didThese to {}
repeat with f in input
set f to (f as text)
if f ends with ":" then
if f is in didThese then --check to see if we did this already
--ignore
else
tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
set didThese to (didThese & f) --load into memory for subsequent iterations of loop
end if
else
--first get containing folder, then use that
tell application "Finder" to set f to ((container of alias f) as alias as text)
if f is in didThese then
--ignore
else
tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
set didThese to (didThese & f)
end if
end if
end repeat
activate application "Terminal"
end run
Gleaned from https://apple.stackexchange.com/questions/112731/automator-service-to-print-a-relative-path-of-selected-files-printing-everything
[edit:]
Incidentally, one thing left to decide is how to treat bundles, like .app files, which aren't really files. Using
set i to info for (alias f)
then
package folder of i
will enable the script to determine this, through an additional if/then branch. I personally don't mind it "cd"ing into bundles.

Applscript *.type

I know I'm being an idiot here... Where am I going wrong? is it neccicary to use sudo rm -f? and why is the *.cr2 not working?
tell application "System Events"
try
do shell script "rm /Users/splashretouch8/Pictures/SplashNW/Capture/*.cr2"
end try
end tell
Here is another approach:
set theFolder to alias ("/Users/splashretouch8/Pictures/SplashNW/Capture/" as POSIX file)
tell application "Finder" to delete (every file of theFolder whose name contains ".cr2")
Or
do shell script "cd '/Users/splashretouch8/Pictures/SplashNW/Capture/' ; rm *.cr2"
Put a "\" character in front of the asterix (e.g. "\*.cr2") and see if it works better for you.
Myself, I think using "rm" is way dangerous in an Applescript. It might be smarter to move files you want to remove into the Trash.

Resources