Applescript: Need Help Resolving - Can't Get every file of said folder? - applescript

With the help of some users on this form I was able to create this script below which lets you choose a folder then choose which folders within the folder you want to rsync into a OneDrive Backup Folder.
Basically were migrating from a on site network storage to OneDrive for Business and want to create a script that as easy as possible for our users.
The issue I have is I dont want the users to be able to choose the original folder, I want to set the variable up front.
In the script I use:
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?
set OuserName to do shell script "whoami"
set userName to do shell script "echo " & OuserName & " | tr a-z A-Z"
tell application "Finder"
if not (disk userName exists) then
mount volume "SMB Server/" & userName & "$"
end if
delay 2
set theDialogText to "
- Mac H-Drive Migration Tool -
This Application Will Migrate a Copy of Your H Drive Data
to your OneDrive for Buisness Folder Locally on Your Mac
Migration Backup Location:
/Users/" & OuserName & "/OneDrive Folder/H-Drive Migration Backup
** Important **
In the Next Window Please Choose
The Root Folder of Your H Drive
The Drive Label Should Look Like: " & userName & "$"
display dialog theDialogText
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
do shell script "mkdir -p ~/'OneDrive Folder'/'H-Drive Migration Backup'"
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
set AppName to "OneDrive.app"
tell application "Finder" to set Answer_ to exists application file ((path to applications folder as string) & AppName)
if Answer_ is false then
beep
beep
beep
beep
beep
end if
delay 1.5
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
end tell
repeat with thisName in theChosenNames
tell application "Terminal"
do script ("rsync -avpz --delete " & (quoted form of POSIX path of ((theFolder as text) & thisName)) & space & (quoted form of POSIX path of HDriveBackupFolder)
end tell
end repeat
end tell

You asked:
"How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?"
Also, just prior to that, you said:
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
First, lets address setting the value of the theFolder variable. It should be as follows:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
I assume the error is being thrown at the following point in the code:
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
Change set theFolderNames to name of folders of theFolder to:
set theFolderNames to name of folders of container theFolder
To test this, I mounted a volume named MYERSMI5$ at /Volumes/ and created some folders within /Volumes/MYERSMI5$/. Then running the following code:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
tell application "Finder"
activate
set theFolderNames to name of folders of container theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
end tell
It produced a list box, containing the names of the folders I created at that location, to choose from.
I did not try to run the entire block of code you included in your question, so if you're having other issues, you'll need to follow up after making the changes mentioned in my answer. There are good reasons why questions involving debugging code should conform to How to create a Minimal, Complete,and Verifiable example.
You may also want to review the Variables and Properties section in the AppleScript Language Guide.

Related

Apple Script: Copy file (being selected from prompt) to another location

I am struggling with my Apple Script. I am asking to select a file and the selected file needs to be copied to another location. I am new to Apple Scripting, so probably made some mistake. I tried different versions with "copy" instead of "duplicate" or "alias" instead of "file", but nothing worked so far. Hope, somebody can help me figure this out.
This is what I scripted so far (I get an AppleEvent timed out):
set DefaultPath to POSIX file "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Test"
set DestFolder to "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Destination"
set theFile to (choose file with prompt "Select file:" default location (DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell
The problem is that the Finder doesn't support POSIX paths.
I recommend to use a relative path path to library folder from user domain and HFS paths (colon separated)
To satisfy the location parameter of choose file put the alias keyword in front of the (HFS) path
set cloudDocs to (path to library folder from user domain as text) & "Mobile Documents:com~apple~CloudDocs:"
set DefaultPath to cloudDocs & "FOLDER:Test:"
set DestFolder to cloudDocs & "FOLDER:Destination:"
set theFile to (choose file with prompt "Select file:" default location (alias DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell

AppleScript - Change all app icons in a directory

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

applescript Posix chaos

Yet again flummoxed by syntax and ordering of an applescript workflow solution
When I create the following code
tell application "Finder"
set remote_p to alias (POSIX file '/Volumes/WAM XSAN/Audio/AAA)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
Everything works fine. However, I need to create the main_folder in different locations dependent on the input "client_code" and "department", So i tried this:
tell application "Finder"
set x_san to "/Volumes/WAM XSAN/"
set x_sannewpath to (x_san & department & "/" & client_code)
set x_sanfolder to POSIX file x_sannewpath
set remote_p to alias (POSIX file x_sanfolder)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
And the error comes back "Can't get "Volumes/WAM XSAN/Audio/AAA" of Application Finder"
Where am i going wrong in setting up the POSIX paths?
Please help!!!!
Try this example. I tried to model it after what you're trying to do. Note that I have moved most of the code outside the Finder tell block of code. The Finder does not know the "posix file" command. That's an applescript command, not a Finder command. Also you don't need the Finder to set variables and add strings together. We can do all of that outside the Finder.
Even though it sometimes works when you have the "posix file" command inside a Finder tell block of code, it's always best if you only tell applications to do what they know how to do. You can find what an application knows how to do by looking in its applescript dictionary. Check the Finder dictionary and you will see that "posix file" is not one of its commands.
Anyway, this will create a folder on your desktop. I hope this helps.
set x_san to "/Users/hmcshane/"
set client_code to "Desktop"
set temp_name to "test folder"
set x_sannewpath to x_san & client_code
set applescript_x_sannewpath to POSIX file x_sannewpath
tell application "Finder"
set main_folder to (make new folder at applescript_x_sannewpath with properties {name:temp_name}) as alias
end tell
Try:
set department to "Audio"
set client_code to "AAA"
set temp_name to "New Folder"
set x_san to "Volumes/WAM XSAN/"
set x_sannewpath to x_san & department & "/" & client_code
tell application "Finder" to set main_folder to (make new folder at POSIX file x_sannewpath with properties {name:temp_name})

Remove multiple PDF passwords with workflow or applescript in a folder

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

does System Events in AppleScript support duplicating files?

I've been trying to get System Events to duplicate files in AppleScript and I've been failing :) I eventually always get the error "error "Files can not be copied." number -1717". So I changed my tactics and tried using the Finder to make sure what i was trying to do was correct. Here is the code that works:
tell application "System Events"
set desktopFolder to (path to desktop folder) as string
set fullPath to desktopFolder & "Temp Export From DO"
set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
set source to path of DOEntry
log "Source file: " & source
set destination to fullPath as string
log "Destination folder: " & destination
tell application "Finder"
duplicate file source to folder destination with replacing
end tell
end repeat
end tell
If I remove that last tell, so that it uses System Events, I get the same error noted above. The dictionary for System Events standard suite has a "duplicate" command so I'm not sure what is going on here. Also, "Learning AppleScript, 3rd ed" from APress notes:
"One particularly annoying omission in System Events is that it can’t yet duplicate files and folders; if you need to do this, the Finder is your best bet."
The 3rd edition is from 2010. It would seem that even in Mountain Lion this is still true. Can anyone confirm this? The 1717 error number lists everywhere else as a handler error and i'm not using handlers.
Unfortunately, you cannot duplicate files using System Events - you have to use the Finder. Even in the answer provided by adayzdone, System Events is not actually handling the duplication.
This looks like it's working (because it's inside a System Events tell block)...
tell application "System Events"
duplicate myFile to myFolder
end tell
...but if you inspect the event log you'll see that the Finder is actually performing the duplication. Behind the scenes, you are passing two Finder objects to System Events. System Events doesn't know how to handle Finder objects, so execution is passed to the objects' owner, the Finder, which executes the command.
For file duplication in AppleScript, you are unfortunately limited to using the Finder or the command line via do shell script.
Try:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
log "Source file: " & DOEntry
log "Destination folder: " & desktopFolder
tell application "Finder" to duplicate file DOEntry to desktopFolder with replacing
end repeat
If you don't need to log the values you can simply:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
tell application "Finder" to duplicate theDOEntries to desktopFolder with replacing
Or:
set desktopFolder to quoted form of (POSIX path of (path to desktop folder as text) & "Temp Export From DO")
do shell script "find '/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries' -name \"*.doentry\" -type f -print0 | xargs -0 -I {} cp -a {} " & desktopFolder
Getting back to your question, the duplicate commands creates duplicates of Finder items. You can use System Events to duplicate Finder items like this:
tell application "Finder"
set myFile to file ((path to desktop as text) & "Test File.txt")
set myFolder to folder ((path to desktop as text) & "Test Folder")
end tell
tell application "System Events"
duplicate myFile to myFolder
end tell

Resources