Run several Applescripts from within current script - applescript

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

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 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

Relative file paths with Applescript

I'm trying to make an Applescript that will open a file on a user's computer without knowing the hard drive or user name, presuming the file is in the same place in the user directory.
tell application "Finder" to open "/Users/jim/Dropbox/Getting Started.pdf" as POSIX file
works great, whereas
tell application "Finder" to open "~/Dropbox/Getting Started.pdf" as POSIX file
fails.
Is there any way to accomplish this simply?
You can't use tilde paths in AppleScript basically because POSIX file is in fact an URL. URLs for file paths doesn't support incremental paths, only absolute paths. But the meaning of the tilde in POSIX paths is not something special, it's just replaced by the home folder. SO to get the same results we only need to change your code to:
tell application "Finder" to open (POSIX path of (path to home folder)) & "Dropbox/Getting Started.pdf" as POSIX file
To accomplish this, you could use a shell script instead of tell application "Finder", or you could use a shell script to get the home folder and insert it into your tell block.
To use a shell script, you can use the following code: do shell script "open ~/Dropbox/Getting\\ Started.pdf. To insert a shell script into your Finder tell block, you could use this code: tell application Finder to open (do shell script "echo $HOME") & "/Dropbox/Getting Started.pdf". This uses a shell script to print the path to the logged in user's home directory and uses it in the path you give Finder.
I hope these suggestions help you solve your problem! ✌️ πŸ‡ΊπŸ‡¦

AppleScript choose file or folder

can I use AppleScript to choose either file or folder in one time?
Now I could use
tell application "SystemUIServer" to return POSIX path of (choose file)
or
tell application "SystemUIServer" to return POSIX path of (choose folder)
to get file or folder. However I cannot get file or folder in one time.
No, you can't do it with "choose file" or "choose folder" verbs, but choosing a file or folder (or multiple files/folders) is supported by the underlying NSOpenPanel. So you can do it with AppleScriptObjC. Here's an example using ASObjCRunner (derived from here):
script chooseFilesOrFolders
tell current application's NSOpenPanel's openPanel()
setTitle_("Choose Files or Folders") -- window title, default is "Open"
setPrompt_("Choose") -- button name, default is "Open"
setCanChooseFiles_(true)
setCanChooseDirectories_(true)
setAllowsMultipleSelection_(true) -- remove if you only want a single file/folder
get its runModal() as integer -- show the panel
if result is current application's NSFileHandlingPanelCancelButton then error number -128 -- cancelled
return URLs() as list
end tell
end script
tell application "ASObjC Runner"
activate
run the script {chooseFilesOrFolders} with response
end tell
ASObjCRunner converts a NSArray of NSURL objects into an AppleScript list of files; the results can look something like:
{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
Firstly, you don't need a tell for that.
POSIX path of (choose file)
Secondly, it is not clear why you need this. Do you mean you want to select a file and it's folder? That's not how you do it; you select the file then parse the file path for the containing folder or use one of the many methods to do that, like
set f to (choose file)
set posixF to POSIX path of f
tell application "Finder" to set filesDir to container of f as alias as text
set posixDir to POSIX path of filesDir
{f, posixF, filesDir, posixDir}
If you want to be able to select multiple folders and files at the same time, I don't think there is a "pure applescript" way to do this (aside from using a drag-drop aware script application).

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