Applescript to select Move or Copy and to select Destination - applescript

I'm somewhat knowledgable about Automator, but not at all knowledgable about Applescript. So, I'm trying to create an Applescript to do the following, since there’s likely no way to do it natively using only Automator.
Instead of creating numerous Services that would all appear in the Services menu like this:
Copy to Destination A
Move to Destination A
Copy to Destination B
Move to Destination B
Copy to Destination C
Move to Destination C
I’d like to create one Automator that would do the following. As soon as the Workflow is started, I’d get the following options:
Move or Copy
Once I make a selection, I’d get the following menu: Select one of the following:
Destination A
Destination B
Destination C
Once I select a destination and click OK, my file would either to moved or copied to the selected destination. What would be a good way to do this using Applescript?

In pure Applescript, you can use "display dialog " for this. You could also use choose from list or chose folder.
set theMethod to button returned of (display dialog "Move or Copy" buttons {"move", "copy"})
set theDestination to button returned of (display dialog "Select your destination" buttons {"Destination A", "Destination B", "Destination"})
-- or use choose folder:
--set theDestination to (choose folder with prompt "Select your destination")
-- or use choose from list:
--choose from list {"Destination A", "Destination B", "Destination"}
display dialog (theMethod & " to " & theDestination)

Related

Delete all files in one folder via Apple Script

I worked out two scripts to do that, but none of them is working (I even don't get shown the "on error" message. Maybe it's because of the pre-defined folder as a variable? As for simplicity and a cleaner code I didn't want to write the complete path again, so I used that folder variable. In case it is relevant, the DataHubFolder variable was defined like this earlier: set DataHubFolder to cloudDocs & "Current Projects:Data Hub:"
Here are the codes I used:
Code #1:
try
tell application "Finder"
set f to every file ¬
of folder ¬
of DataHubFolder & "Distrokid:"
delete f
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Code #2:
try
tell application "Finder"
delete (every item of folder (DataHubFolder & "Distrokid:"))
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Would be so happy about some tips.
Thanks a lot.

Directing an app to run off of a different script in AppleScript.

I'm making an applescript to help with school. You type the subject you wish to access, and then it opens that folder (inside the app). However, some subjects have ebooks, so I want there to be another dialog box asking you wether you want to open the ebook or the folder. Unfortunately, a display dialog can't branch off of another display dialog.
So far, the only way I've found around this is to direct the app to run off of another script (in the "Scripts" folder of the app). I've tried
tell application "Script Editor" to run script (path to me as sting) & "Contents:Resources:Scripts:Subject.scpt"`
But it didn't work. Sorry if I'm barking up the wrong tree and seem completely stupid. Thanks
As far as I understood what you're trying to achieve, the following script would work.
set listOfSubjects to {"Math", "Biology", "History", "Languages"}
set subjectsWithEbook to {"Biology", "History"}
choose from list listOfSubjects with prompt "Select the subject"
set subject to item 1 of result
set openEbook to false
repeat with subjectItem in subjectsWithEbook
if subjectItem contains subject then
choose from list {"Open folder", "Open Ebook"} with prompt "What to open?"
set openType to item 1 of result
if openType contains "Open Ebook" then
set openEbook to true
end if
exit repeat
end if
end repeat
if openEbook then
--Open ebook
else
--Open folder
end if
I am guessing that you want to type the subject name? Here is an example that uses dialog boxes.
set subjectFolderPath to (path to me as string) & "Contents:Resources:Subjects:"
set subjectsWithEbook to {"English", "Spanish"}
display dialog "What subject would you like to access?" default answer ""
set theSubject to text returned of the result
if theSubject is in subjectsWithEbook then
display dialog "Would you like to open the Folder or the Book?" buttons {"Folder", "Book"} default button 1
set toOpen to button returned of the result
if toOpen is "Book" then
-- tell app "Preview" to open ???
beep
else
tell application "Finder" to open folder (subjectFolderPath & theSubject & ":")
end if
else
tell application "Finder" to open folder (subjectFolderPath & theSubject & ":")
end if
You might also want to have a list of subjects that are allowed and error check for them.

What is the correct syntax for the `add` command in AppleScript's library for Automator?

Goal
I would like to use AppleScript to open Automator and add specific actions to a new Workflow, regardless of the localization settings (language). In other words, I would like to use AppleScript to open a new Automator workflow and add specific actions in a specific order (at indexes) using the bundle identifiers of each action (to avoid language issues) or some other way to avoid language issues.
The Problem
The problem is that I cannot get Automator to add anything to the workflow.
What is the syntax for the add command?
Can I use bundle ids to add actions?
So far, this is what I have:
tell application "Automator"
make new workflow with properties {name:"Merge PDF Files"} -- Skips Opening Dialog
add "Get Specified Finder Items" to workflow 1 at index 1 --("com.apple.Automator.SpecifiedFiles")
add "PDF-Seiten kombinieren" to workflow 1 at index 2 --("com.apple.action.joinpdf")
end tell
Complete Code for a Merge PDF Files Droplet
After bwaldie posted his answer, I would like to share my useful droplet. You can put it on any Mac running OS X (especially useful for people who aren't familiar with Automator). To use it, just paste the code into the AppleScript Editor and save it as an application.
See code for droplet:
on open the_Droppings
-- CONVERT INPUT LIST OF ALIASES TO POSIX PATHS
repeat with itemStep from 1 to count of the_Droppings
set item itemStep of the_Droppings to POSIX path of item itemStep of the_Droppings
end repeat
tell application "Automator"
activate
set theWorkflowName to "Merge PDF Files"
set msg to "no"
tell application "Finder" to if exists POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string) as POSIX file then set msg to "yes"
if msg is "no" then
set myWorkflow to make new workflow with properties {name:theWorkflowName, path:POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string)}
add (first Automator action where its id = "com.apple.Automator.SpecifiedFiles") to myWorkflow at index 1
add (first Automator action where its id = "com.apple.action.joinpdf") to myWorkflow at index 2 --("com.apple.Automator.SpecifiedFiles")
else
set myWorkflow to open POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string)
end if
set actionsList to name of Automator action of myWorkflow
set firstAction to item 1 of actionsList
tell myWorkflow
set value of setting of Automator action firstAction to the_Droppings -- MUST BE LIST OF POSIX PATHS
end tell
end tell
end open
Try something along the lines of...
set theWorkflowName to "Merge PDF Files"
tell application "Automator"
set myWorkflow to make new workflow with properties {name:theWorkflowName, path:POSIX path of ((path to desktop folder as string) & theWorkflowName & ".workflow" as string)}
save myWorkflow
add (first Automator action where its id = "com.apple.Automator.SpecifiedFiles") to myWorkflow
end tell
I think the key is to create the workflow, then save it, and then add the action.
Hope this helps!
-Ben
If you want to use Automator, there is no need for scripting. This works for me:
start Automator;
create new Service or e.g. create new Workflow;
if you created a Service, select "Service receives PDF files in
Finder.app" at the top of the window; if you chose Workflow, select
Files & FOlders in the list of Actions on the left and drag Get
Selected Finder Items to the right and make sure that Appending Pages is selected;
Select PDFs in the Actions list and drag Combine PDF Pages to the right
Select Files and Folders in the Actions list and drag Copy Finder Items to the right and make sure that Desktop is selected
Drag "Make Finder Item Names Sequential" to the right, select Make Sequential" and "Add number to existing item name".
You may want to tweak and experiment with Automator a bit to make sure that it does exactly what you want.

AppleScript "Save As" HTML in TextEdit

I want AppleScript to loop through a set of of RTF files in folder and save them as HTML files.
This is my simple code so far. The XXXX is where I'm struggling:
tell application "Finder"
set source_folder to choose folder
set aList to every file in source_folder
repeat with i from 1 to number of items in aList
tell application "TextEdit"
set aFile to (item i of aList)
save as aFile XXXXXXXXX
end tell
end repeat
end tell
I'm really new to this... any help much appreciated.
You don't need TextEdit for this. There is a command line program textutil which will do the job without all the opening and saving stuff required with TextEdit. We can fix your TextEdit script (it has a few errors) but try this first and let us know if it does the job for you. The html files will have the same name but with the html extension and will be located in source_folder. The ouput path can be changed in the code by using the "-output" switch of textutil. See "man textutil" if you want to look at everything it can do.
And a general question... what is a RTD file? Do you mean rtf or rtfd? Textutil will work with rtf/rtfd but not rtd, so I hope that isn't really your file type.
set source_folder to choose folder with prompt "Choose a source folder."
set output_folder to choose folder with prompt "Choose an output folder."
tell application "Finder"
set theFiles to (files of entire contents of source_folder) as alias list
end tell
repeat with aFile in theFiles
tell application "Finder"
set fileName to name of aFile
set fileExt to name extension of aFile
end tell
set outputPath to (output_folder as text) & text 1 thru -((count of fileExt) + 1) of fileName & "html"
do shell script "/usr/bin/textutil -convert html -output " & quoted form of POSIX path of outputPath & space & quoted form of POSIX path of aFile
end repeat
You mention you are new to applescript, so I'll give you some general pointers you should keep in mind when writing applescript code.
Avoid putting tell blocks of code inside each other. You have tell app TextEdit inside tell app Finder. That's bad. Doing this is a source of many conflicts because you are basically telling the Finder to tell TextEdit to do something. That's not good because commands can get confused and it's really hard to debug these kinds of issues. So keep your tell blocks separate.
Avoid telling an application to perform a command that is not in its applescript dictionary. You should only tell an application to do commands that it knows and an application only knows about the commands in its dictionary. So for example, you are telling the Finder to "choose folder". The Finder does not know that command. That's an applescript command. So doing as you have done is another possible source of errors. In this case that's a simple command and it will work but in general avoid doing this.
Regarding the Finder, you should avoid using it too much. The Finder is a major program on your computer and is often busy doing computer related stuff. As such it's best to only use it when necessary. As an example you can see in my code that I removed the "choose folder" and the repeat loop from the Finder. I purposely appended "as alias list" to the end of the Finder command to make the list of files usable outside of the Finder tell block of code. Of course use the Finder if needed but it's best to not use it if you don't need it.
Use the applescript dictionary of your applications. As mentioned above, the dictionary lists all of the terms and the syntax that an application understands (granted the dictionaries are difficult to understand but you will get better at it the more you use them). Under the file menu of AppleScript Editor choose "Open dictionary" and a list of all the applications that understand applescript is shown. Choose an application from that to see its dictionary. So for example, you are trying to figure out TextEdit's "save as" command. You can usually get good direction from the dictionary so you should take a look at that. Use the search field to search!
So I hope that helps! Good luck.

Changing icon size in Applescript "choose file" window?

Is there a way to change the icon size in a "choose file" preview window? I'd like to make my preview thumbnails larger.
Here's the code I'm using. Pretty basic:
set theFile to (choose file of screenFolder with prompt "Select a file to read:" of type {"JPG"})
Thanks!
As far as I know, you can't change the size of the preview window (I apologize for any inconvenience).
Note
Your code won't compile. I'm assuming you want screenFolder to automatically be opened. If that is true, then use the default location property, which automatically navigates to the desired folder. Your new code should look something like this...
set theFile to (choose file with prompt "Select an image to read:" default location screenFolder of type {"JPG"})

Resources