Set theFolder to (Macintosh HD:Users:jr:Desktop:Dsource)
Set rnd to (random number from 1 to 3)
Set myScript to load script file (rnd) of theFolder
Run script myScript
This is the script I am attempting to use to open random script files from a folder and execute them. I have written 3 or 4 different types of scripts to try and do this but none seem to work. Can someone point out the error or show me how to do this. Thanks
==> 1. make a folder on your desktop named "Dsource"
==> 2. create a script and name it "1". The script shall contain display dialog "Script 1" so when you start it it shows the script-number. Save/put it into the Dsource folder and repeat that (but change the number in the display dialog) until you have 3 scripts. Note: often the file name extension is hidden in Finder but such scripts have the extension ".scpt".
If above things are given this should work:
run script (load script file (((path to desktop) as text) & "Dsource:" & (((random number from 1 to 3) as text) & ".scpt")))
ADDITION:
Longer version which does the same but does it step by step:
try
set pathToMyFolderOnDesktop to (path to desktop as text) & "Dsource:" as alias
set rnd to (random number from 1 to 3)
set rndFileName to (rnd as text) & ".scpt"
set FullPath to pathToMyFolderOnDesktop & rndFileName as text
set myScript to load script (FullPath as alias)
run script myScript
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
return
end try
This also displays an error if - for example - the folder isn't there (-43).
Strings aren't folders in Applescript
You are trying to access files using a string. "Macintosh HD:Users:jr:Desktop:Dsource" is not a folder until you make it one. So, this line
Set myScript to load script file (rnd) of theFolder
is getting the rnd character from the string (i.e. M if rnd is 1) and trying to do load script on it.
To make a string into something that Applescript recognizes as a folder, you can add alias to the front so it's alias "Macintosh HD:Users:jr:Desktop:Dsource"
You need to target a file
Assuming, you could get all the files of a folder with the following line, your logic is still a little off.
Set myScript to load script file (rnd) of theFolder
This line would be targeting a file named rnd. Not the rnd file of the folder. In other words, this line evaluates to
Set myScript to load script file "Macintosh HD:Users:jr:Desktop:Dsource:1"
The easy way to do it
Applescript provides the some command, which allows you to choose a random item from a list
some item of {1, 2, 3}
--> 2
some item of {1, 2, 3}
--> 1
So, gather your existing script files in a list, and choose a random one from that list.
tell application "System Events"
set scriptFiles to files of alias "Macintosh HD:Users:jr:Desktop:Dsource:" whose kind is "Compiled OSA Script"
end tell
if (count scriptFiles) > 0
set myScript to load script (some item of scriptFiles)
run myScript
else
display dialog "No script files were found."
end if
Related
I am trying to make an Applescript that will launch Photoshop and call the Automate Batch function to run a specific Action. I have zero experience doing this and have only gotten snippets of code from my searching. I was wondering if someone could help me on this.. in particular if it is at all possible to pass a source folder to the batch call and then how to make the Batch call.
In particular, I am having issues trying to figure out how to:
Pass the source folder into the Batch Options
Call a specific Action in the Batch_Options from my Photoshop
Run the batch call with these options
I've updated with the latest code that is partially there...
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "Save as Photoshop PDF"
activate
try
set Batch_Options to {class:batch options, destination:save and close, error file:Error_File, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
batch "Save" from files folderList from action_set_name with options Batch_Options
end try
end tell
output:
"File Names are: Macintosh HD:Users:Joe:Desktop:Temp:Creature01_CO_v003.psdMacintosh HD:Users:Joe:Desktop:Temp:SecretLaboratory.psd"
Photoshop opens, and then nothing happens...
The following seems to work in the Automator:
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
#Alias list needs to be converted to a string for it to work in Photoshop batch call
set FileNames to {}
repeat with i from 1 to the count of folderList
set current_file to item i of folderList
copy current_file as text to the end of FileNames
end repeat
#Setup error log
set err_log to (sourceFolder as string) & "Error_Log.txt"
if not (exists file err_log) then
make new file at sourceFolder with properties {name:"Error_Log.txt"}
end if
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "SetA"
activate
set Batch_Options to {class:batch options, destination:save and close, error file:err_log, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
#First arg is the Action Name, second is the list of files, third is the Action Set Name
batch "ActionA" from files FileNames from "SetA" with options Batch_Options
end tell
I have folders containing applications. I want to be able to select the folder with an apple script, and have the script go through each app file in that directory, changing the icons for me.
I want to have the icon set from an image stored in the script directory.
I'd really appreciate any help because I've been trying to make this work for a while. This is my progress so far:
property appcurrentCount : 0
on run
set theFolder to (choose folder with prompt "Select the start folder")
doSomethingWith(theFolder)
end run
on doSomethingWith(aFolder)
tell application "Finder"
set subApps to every file of aFolder
repeat with eachFolder in subApps
-- replace icon here somehow
end repeat
end tell
display dialog "Count is now " & appcurrentCount & "."
end doSomethingWith
The script bellow does now what you want. As explained before, your icon file must be type icns. I now add this filter directly in the choose file command.
The selected icon will now replace ALL icons already in each Contents/Resources folder of all applications in the selected folder.
The replacement will be done preserving the name of the icns already in place.
Warning : there is no 'undo' command. your old icons are overwritten !!
set Myicon to choose file with prompt "Select Icns to be copied in every Application of the folder" of type "com.apple.icns"
set MyFolder to choose folder with prompt "Select the folder with all applications to be changed"
set Source to POSIX path of Myicon -- convert path to unix form
tell application "Finder"
set MyApps to every item of MyFolder whose name extension is "app"
display dialog "count apps=" & count of MyApps
repeat with anAps in MyApps -- loop for each App
set IcnFolder to ((anAps as string) & ":Contents:Resources:") as alias
set MyIcns to (every item of IcnFolder whose name extension is "icns")
display dialog "count of icn in " & alaps & " = " & (count of MyIcns)
repeat with oneIcon in MyIcns -- loop for each icns
set Destination to POSIX path of (oneIcon as string)
try
do shell script "cp " & (quoted form of Source) & " " & (quoted form of Destination)
end try
end repeat -- loop for each icns
end repeat -- loop for each App
end tell
I'm trying to write the current file path in the comment text box; the one you see when you right-click and Get Info. I'm pretty close, because I got it to write a file path to all the files inside a folder, but all the file paths are the same for some reason.
For example, when I right click a file after I run the script and see the comment, it might be "'/Users/Admin/Desktop/automator test/folder/spikyBall#2x copy 4.png'" and it will be that for all the files.
My Shell Script variable is defined as:
bashFilePath=$(osascript -e 'tell application "Finder" to set filePath to quoted form of posix path of (item 1 of (get selection) as text)');
echo $bashFilePath;
If I am interpreting your question correctly, try this:
echo $1
In shell scripts, $1 is the first argument ($2, $3, etc. also work). So the above just echoes the first argument... which with your automator program is the path of the file.
I ended up using pure Apple script for this. Select any files and folders that you want and run the script. It can also be made into a files or folders Service in Automator.
tell application "Finder"
activate
set fileList to selection
if (count result) is 0 then
try
get (target of front Finder window) as alias
on error
choose folder with prompt "Set comments of files in this folder:"
end try
try
set theFolder to result
set fileList to every file of folder (result) as alias list
end try
end if
display dialog "How would you like to set comments?" buttons {"Overwrite", "Cancel"} default button 2 with title "Set Spotlight Comments current to file path"
set userInput to the result
set itemNum to 1
if (button returned of userInput) is "Overwrite" then
if (class of first item of fileList) is alias then
set comment of every file of folder theFolder to POSIX path of (item itemNum of (get selection) as text)
set itemNum to itemNum + 1
else
repeat with thisFile in fileList
set comment of thisFile to POSIX path of (item itemNum of (get selection) as text)
set itemNum to itemNum + 1
end repeat
end if
end if
end tell
How do I remove passwords from multiple PDF files using Applescript or by creating a Workflow in OS X?
My scenario is that I have multiple password protected PDF files in a folder. I know the passwords for all, which is same. I want to be able to run a Workflow on this folder so that all PDFs inside it are unlocked by the workflow.
OR run an Applescript shell code on all these files at once
I also preferably want to be able to create a way where putting / moving / pasting any PDF in the folder automatically unlocks it :)
Help appreciated !!
Update:
I have tried pdftk. The following code works awesome in Terminal, once pdftk is installed
pdftk secured.pdf input_pw foopass output unsecured.pdf
Now I want to be able to create a workflow that runs this command on selected files or on all the files in a folder
The AppleScript command to execute a shell script is do shell script...
So something like this:
do shell script "pdftk secured.pdf input_pw foopass output unsecured.pdf"
should work.
At this point I see 2 options:
write an AppleScript script that ask the user for the folder or get it from the Finder selection and then execute the command for each file in the folder;
write an Automator workflow that get the files from the folder using already available actions and then attach a new action that execute the AppleScript script.
For option 2 you can set an Automator workflow as in the following image.
Have you heard of "Folder Actions"? It's a way to attach an applescript to a folder so that whenever a new file is added to the folder the applescript is run. A quick google search turned up this which will give you directions on how to set it up. You can do more google searching if you still have questions.
Here's an applescript you can use with folder actions. I didn't test it but it should work (it's basic code). This will do its stuff on only pdf files. Other files you add to the folder will be left alone. NOTE: you have to put in your values for the first 4 variables of the script.
Good luck.
on adding folder items to theFolder after receiving theItems
-- enter your values here
set pdftkPosixPath to "/usr/bin/pdftk"
set pWord to "foopass"
set appendedName to "_unlocked" -- text to append to the file name
set shouldTrash to true -- true or false, move the locked file to the trash after unlocking?
set fContainer to theFolder as text
repeat with anItem in theItems
try
tell application "System Events"
set fName to name of anItem
set fExt to name extension of anItem
end tell
if fExt is "pdf" and fName does not contain appendedName then
set baseName to (text 1 thru -5 of fName) & appendedName & ".pdf"
set newPath to fContainer & baseName
do shell script (quoted form of pdftkPosixPath & space & quoted form of POSIX path of anItem & " input_pw " & quoted form of pWord & " output " & quoted form of POSIX path of newPath)
if shouldTrash then
tell application "Finder" to move anItem to trash
end if
end if
end try
end repeat
end adding folder items to
EDIT: here's how you can ask for a password. Note that if you want to see the text then remove "with hidden answer".
display dialog "Enter a password:" default answer "" with icon note with hidden answer
set theAnswer to text returned of the result
if theAnswer is not "" then set pWord to theAnswer
In Script editor i have written a command to boot the JAR.
do shell script "cd /Desktop/RunJar/; java -jar RunMyJar.jar"
and Saved as script file as a Application. When i click on script file jar get run.
My requirement
I would like get the name of file that has been dropped onto the script file and would like to pass the name of that dropped file as an argument to my jar.
I have implemented this in Windows but could not working similar on MAC O.S.
On Windows
I have placed a BAT file to Boot JAR along with the absolute file name, that has been dropped on the bat file on windows. #echo %* will give me the list of files that has been dropped onto Batch file.
#echo %*
#pause
java -jar RunMyJar.jar %*
Similar i would like to implement in MAC O.S.
Thanks.
I found the following example at: Ben's AppleScript Snippets:
on open of finderObjects -- "open" handler triggered by drag'n'drop launches
repeat with i in (finderObjects) -- in case multiple objects dropped on applet
displayName(i) -- show file/folder's info
if folder of (info for i) is true then -- process folder's contents too
tell application "Finder" to set temp to (entire contents of i)
repeat with j in (temp)
display dialog j as string -- example of doing something with each item
end repeat
end if
end repeat
end open
You can also easily modify my answer to a similar question:
on open of theFiles -- Executed when files are dropped on the script
set fileCount to (get count of items in theFiles)
repeat with thisFile from 1 to fileCount
set theFile to item thisFile of theFiles
set theFileAlias to theFile as alias
tell application "Finder"
set fileInfo to info for theFileAlias
set fileName to name of fileInfo
-- something to this effect, but now that you have the file name,
-- do what you will...
do shell script "cd /Desktop/RunJar/; java -jar " & fileName
end tell
end repeat
end open
To add to Paul's answer
on open of finderObjects
repeat with currFile in finderObjects
-- ok, we have our current item. But it's an "alias": a Mac OS path
-- not a POSIX path
set unixPath to POSIX path of currFile
set base to do shell script "dirname " & unixPat
set fname to do shell script "basename " & unixPath
-- you could ask the Finder to give this to you too -- I'll use this way because
-- I'm guessing it's more familiar to you
do shell script "cd '" & base & "';" & " java -jar " & fname
end repeat
end open