Stuck in a loop while password protecting a folder - applescript

I wanted to make a folder action that requires a password to access the contents upon opening, and the current code is as follows:
on opening folder
set login to true
if login = true then
tell application "Finder"
close windows
end tell
set passw to display dialog ¬
"Enter your password:" default answer ¬
"" buttons {"Cancel", "Let me in!"} ¬
default button 2 ¬
giving up after 5 with hidden answer
set entered to text returned of passw
if entered = "password" then
tell application "Finder"
open folder "Myfolder"
end tell
end if
end if
end opening folder
However, when Myfolder is opened, it obviously requires a password, and even when I enter it correctly, the window opens for a second and then closes, to require another entry of the password, and this continues on and on.
I presume this is because every time Myfolder is opened the script runs and requires a password, but how do I get around this? I have tried setting login to false after the open folder "Myfolder" and end tell but this does not word as login is set to true every time the script is run.
How can I stop the script requiring a password forever?

You can try something like this:
property passwordWasGiven : false
on opening folder theFolder
if passwordWasGiven is false then
tell application "Finder"
close theFolder
end tell
set passw to display dialog ¬
"Enter your password:" default answer ¬
"" buttons {"Cancel", "Let me in!"} ¬
default button 2 ¬
giving up after 5 with hidden answer
set entered to text returned of passw
if entered = "password" then
set passwordWasGiven to true
tell application "Finder"
open folder theFolder
end tell
end if
else
set passwordWasGiven to false
end if
end opening folder
Properties are preserved across script runs, so passwordWasGiven becomes a toggle for whether a correct password was entered.
But please, this is a poor, poor way to password-protect a folder. People can see the contents of the folder for a moment before the Finder closes it; people can access the contents easily on the command line; people can read the script to discover the password. Anyone with half-a-brain can get in. If you want to make a secure folder, use Disk Utility or diskutil on the command line and make an encrypted disk image. That is no more difficult than using a folder — you double-click on it to open it, just like you would with a folder — but the password is secure (assuming you choose to save it in keychain) and the encryption is strong.

Related

Unable to find the application support folder for a specified application inside an Apple Script

I have an apple script that asks the user to select an application as a prompt. I would like to know how it could be possible to find the directory of the Application Support folder for this application per example, Spotify is
~/Library/Application Support/Spotify/
On my mac, but like is there a way inside apple script to find this directory for the specific app. It would really be appreciated, thanks!
Because I would like to then delete this folder using Apple Script
This should accomplish what you're looking to achieve. Be careful though because using System Events to delete... deletes the item permanently instead of sending it to the Trash
property folderNames : missing value
tell application "System Events"
set appSupportFolder to application support folder
set folderNames to name of folders of appSupportFolder
set theChoice to my chooseFolderToDelete()
delete folder theChoice of appSupportFolder
end tell
to chooseFolderToDelete()
activate
set theChoice to (choose from list folderNames with prompt ¬
"Select an Application's Support folder to delete" OK button name ¬
"Delete Folder" cancel button name "Cancel") as text
end chooseFolderToDelete

Automator service in OSX High Sierra

Automator service at the active finder window. The idea is to create a folder template at the active finder window with choosing the new folder
dialog.
Service:
ask for folder name
make new folder
run shell script
The Automator service fails and I am not sure why. The path of the active finder window is not reaching the variable thePath which is assigned to the new folder Automator action.
The screen shots displays a simplified version of the service and the error.
Can I get idea of what is wrong?
Try inserting this code in a run AppleScript action, into your workflow.
set theName to text returned of (display dialog ¬
"INSERT A NEW FOLDER NAME" default answer ¬
"Enter Desired Name Of New Folder" hidden answer false ¬
buttons {"Cancel", "OK"} ¬
default button ¬
"OK" cancel button ¬
"Cancel" with title ¬
"CHOOSE A NAME" with icon 1 ¬
giving up after 30)
tell application "Finder"
set currentTarget to target of window 1 as alias
set the name of (make new folder at currentTarget) to theName
end tell

AppleScript: Error -15267 Copying to External

I recently started AppleScript and wanted to slowly getting into scripting and hoping someone can help me with a block I have ran into. Tried a few things and moving pieces around and changing identifiers but don't know what I should be looking for to change. Hoping someone can help me out and explain my flaws.
Background Story: Trying to test a script to back up of a user account home folder to an external drive.
What I have first: drive verification to make sure there is a drive plugged into the computer. Followed by setting source and destination. Then tried compiling an if then statement to single out 2 folders I don't want to be copied over (only focusing on the user folder, but unsure if that is correct and will need that looked at too). I get my error at the duplicate command: "Finder got an error: An item with the same name already exist in this location." However, there is nothing the external so I have no clue what else to do. Any advice would be helpful.
This is the code I have to start with:
tell application "System Events" to set diskNames to name of every disk
if "Untitled" is in the diskNames then
display dialog "Disk is mounted" buttons {"OK"} default button 1 with icon 1
else
display dialog "No disk found" buttons {"OK"} default button 1 with icon 1
end if
tell application "Finder"
set source_folder to "Macintosh HD:Users:"
set tar_disk to "Untitled:"
if source_folder is not {"administrator", "Shared"} then
with timeout of 3600 seconds -- 2 hours
duplicate source_folder to tar_disk
end timeout
else
display dialog "No folder detected" buttons {"OK"} default button 1 with icon 1
end if
end tell
You are able to copy only your own home folder with the Finder, you don't have permissions to copy the home folders of other users (except Shared).
Basically it's good habit to add Finder specifiers to literal strings representing files, folders and disks for example
set tar_disk to disk "Untitled"
Since there is a home property pointing to the current home folder you can write
tell application "Finder"
set tar_disk to disk "Untitled"
with timeout of 3600 seconds -- 2 hours
duplicate home to tar_disk
end timeout
end tell
There is an important caveat: The Finder copies only the visible files and folders. If AppleShowAllFiles is set to false all files starting with a dot are ignored. To copy the entire home folder something like the shell rsync command is the better choice – also for speed reasons.

Get the path of a closed folder with Applescript

I'm looking for a solution to get with Applescript the path of a folder closed by a user. The aim is to call another scripts with this path in argument.
I already tried to to this, but it seem to not work (my Folder actions are correctly set):
on closing folder window for theAttachedFolder
tell application "Finder"
set thePath to POSIX path of theAttachedFolder
display dialog thePath
end tell
end on closing folder window for
You need to change end on closing folder window for to end closing folder window for, because this block starts with the word on and ends with the word end. In both cases, next to it you must write the name of the action, in this case: closing folder window.
Additionaly, you can move display dialog out of the tell block, because it's not necessary to target the Finder application. display dialog is part of Standard Additions.
Code:
on closing folder window for theAttachedFolder
tell application "Finder"
set thePath to POSIX path of theAttachedFolder
end tell
display dialog thePath
end closing folder window for

Make apple script code work on any laptop (not need the user's name)

I have a simple code of deleting a file and making a folder. But how do I make it run on any laptop because everyone's user name is different. I know it has something to do with "~" but i'm not sure where.
The code below works properly but how do I make sure it can run on any mac:
try
tell application "Finder"
delete ((POSIX file "/Users/myname/Desktop/thefile.txt"))
make new folder at (POSIX file ("/Users/myname/Desktop")) with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try
The syntax independent of the user name is much simpler since the desktop folder of the current user is the root folder of the Finder
try
tell application "Finder"
delete file "thefile.txt"
make new folder at desktop with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try
You can also
set username to ""
tell application "System Events"
set username to full name of current user
end tell
try
tell application "Finder"
delete ((POSIX file "/Users/username/Desktop/thefile.txt"))
make new folder at (POSIX file ("/Users/username/Desktop")) with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try

Resources