Create Apple Script to Map SMB Shares with custom servers - macos

I want to make a applescript that will let the user type in the server name and name of the shared folder and map it via applescript.
I know how to map a set in stone smb share but I want this to be user friendly so they can they type in the share name.
Example
smb://share3/installs
The script would ask what server is it on: share3
Script would ask what is the folder name: installs
Then the script would popup the default connect to server logon info that the user needs to type.

Well, what you're asking for is display dialog and a bit of do shell script, which is detailed in the AppleScript Language Guide (PDF).
So, to answer your question directly, you probably want something like the following:
set server to text returned of (display dialog "Type the name of the server" with title "Open Share" default answer "10.0.10.50")
set share to text returned of (display dialog "Type the name of the share" with title "Open Share" default answer "installs")
do shell script "open smb://" & server & "/" & share
However, if your users are "simple" as you say, then you might not want them typing in server names and share names. Instead, you could provide them with pick lists.
set serverShares to {{"share2", {"installA", "installB"}}, {"share3", {"install1", "install2"}}}
set serverList to {}
repeat with servers in serverShares
set the end of serverList to item 1 of servers
end repeat
set serverChoice to item 1 of (choose from list serverList with title "Open Shared Volume")
set shareList to {}
repeat with i from 1 to length of serverShares
set server to item 1 of item i of serverShares
if server is equal to serverChoice then
set shareList to item 2 of item i of serverShares
exit repeat
end if
end repeat
set shareChoice to item 1 of (choose from list shareList with title "Open Shared Volume")
do shell script "open smb://" & serverChoice & "/" & shareChoice
Just customize the server/share name nested list at the top to customize which shares are available on which servers. Using the shell script open smb://server/share will prompt the user for login credentials if they don't already have them saved in their keychain.

Related

If functions not working in AppleScript when used with lists

I am trying to form a script to open up a different classroom depending on what subject I have but am not finding success.
This is the script I've used.
set theList to {"Maths", "Further Maths", "Physics", "Chemistry"}
set lesson to choose from list theList with prompt "What lesson do you have?"
if lesson is equal to "Further Maths" then
do shell script "open https://classroom.google.com/u/0/c/A"
end if
And so on with the other classes.
choose from list returns always a list (or false if the user presses the Cancel button) even if with multiple selections allowed is not specified.
In single choice mode you have to get the first item of the list
set theList to {"Maths", "Further Maths", "Physics", "Chemistry"}
set lesson to choose from list theList with prompt "What lesson do you have?"
if lesson is false then return
if (item 1 of lesson) is equal to "Further Maths" then
do shell script "open https://classroom.google.com/u/0/c/A"
end if

Applescript - remember state between executions?

Some Applescripts I have used have remembered state between executions, e.g., location for Open/Save dialogues.
Now I have written an AS that takes a string as input from the user (via a display dialog). I would like the script to remember that string between executions. Possible? How?
If you make the string a property, it will remember that string between executions, until the next time you open up and re-save the script.
Here’s an example:
property myName : ""
if myName is "" then
display dialog "What is your name?" default answer ""
set myName to the text returned of the result
end if
display notification "Hello, " & myName
Save the script as an “Application”, “Script”, or “Script bundle”. If saved as “Text” (or if run from Script Editor) it will not maintain properties between runs.
The first time you run it, it will notice that myName is the empty string and request your name.
Subsequent times you run it, it will already have your name in the property myName.
If you open the script again, and then resave the script, this resets all properties. If you need the script to retain data through edits, you’ll need to save it in a database, which could be as simple as a text file in a central location.
set nameFile to POSIX file "/Users/USERNAME/name.txt"
tell application "Finder"
if exists nameFile then
set nameFileHandle to open for access nameFile
set myName to read nameFileHandle
close access nameFileHandle
else
display dialog "What is your name?" default answer ""
set myName to the text returned of the result
set nameFileHandle to open for access nameFile with write permission
write myName to nameFileHandle
close access nameFileHandle
end if
end tell
display notification "Hello, " & myName
In this case the variable nameFile doesn’t need to be a property since it never changes. Because it doesn’t need to be a property, this version also does not need to be saved as non-Text, and can even be run from Script Editor.
It is also very simple; if you need to store and retrieve multiple items, you would need to follow up on the “File Read/Write” section of “StandardAdditions”. You can get there from Script Editor under “File:Open Dictionary…”.
For more complex memory, you may find the “Property List Suite” in the “System Events” dictionary useful.

Applescript to extract subject line

Total n00b here, definitely not a programmer. Would love some assistance with an applescript. I'm basically trying to extract a subject line from emails in a particular folder underneath my inbox. I need it to yank out the subject line, look for some numbers (ex. 123456) and pull out the last 4 digits. Then put that into a text file. Below is what I have so far, but it's not working. I'm not getting any output at all. Any guidance would be greatly appreciated!
tell application "Microsoft Outlook"
set theAccount to exchange account “my account"
set topFolder to folder "Inbox"
set subFolder to folder “Stuff"
set theMessages to messages of subFolder
set folderPath to ((path to home folder from user domain as string) & “emails")
repeat with aMessage in theMessages
my SetSubject(subject of aMessage)
end repeat
end tell
on SetSubject(theSubject)
tell application "Microsoft Outlook"
try
save theSubject in ((path to home folder from user domain as string) & “emails" & “numbers.txt" as string)
end try
end tell
end SetSubject
end
I don't know what your criteria are to filter the subjects.
This is an example to write the last 4 characters of all subjects of the messages in the specified mailbox into a file numbers.txt in folder emails in the home folder, one subject per line.
If an error occurs – for example the number of characters in the subject is less than 4 – the file is closed reliably and the script aborts.
set numbersFile to (path to home folder as text) & "emails:numbers.txt"
tell application "Microsoft Outlook"
set theSubjects to subject of messages of folder "Stuff" of folder "Inbox" of exchange account "my account"
end tell
try
set fileDescriptor to open for access file numbersFile with write permission
repeat with aSubject in theSubjects
write (text -4 thru -1 of aSubject & return) to fileDescriptor starting at eof
end repeat
close access fileDescriptor
on error
try
close access file numbersFile
end try
end try

Automator: Choose from list and getting the corresponding variable value

I'm new to AppleScript but I do have some basic knowledge of Automator. Here is what I want to achieve:
There is a simple list of URLs that I often use throughout my work. Like this:
Product datasheet: URL1
Licensing: URL2
Price list: URL3
...
etc.
In most cases I use these URLs when sending a mail message to a customer, but sometimes I also use them in other applications, like Safari. So the service should be global.
I need the service to prompt me with a list of items where each item is the name of a link (like {"Datasheet", "Price list", "Licensing", etc} ).
When I select an item and click OK, the service must fetch the URL that corresponds to that link name and then put it in the clipboard, so that I can paste it whenever I need to use it.
I followed recommendations in this q&a Automator: How do I use the Choose from List action? and created the first action (Run AppleScript). Also, i created a number of variables and specified names and URLs. I.e. I'm going to store the actual list ("database") in the service. There are somewhere between 30 and 50 links/records in total so I don't probably need an external Excel file or something.
What I can't figure out is how to get/fetch the URL from the variable. The Get Value of Variable doesn't work for me because it requires a constant variable name as input. However, I need an action to receive input from the Run AppleScript action, lookup the variables list and fetch the URL and then pass the resulting URL onto the Clipboard action.
Thank you for your help.
Make an automator service workflow with a single Run AppleScript action, containing the following code:
set x to item 1 of (choose from list {"Product datasheet", "Licensing", "Price list"})
if x is "Product datasheet" then
set the clipboard to "URL"
else if x is "Licensing" then
set the clipboard to "Another URL"
else if x is "Price list" then
set the clipboard to "Yet Another URL"
end if
To add to this, add to the list of items on the first line, copy the else if line and edit it to check for the newly added item, then copy the set the clipboard line and edit it so that it contains the new URL.
Also, make sure that you set the workflow to work in "any application" and you should also set it to receive "no input".

Having trouble with remote folder actions with AppleScript

So I have an Applescript to help with naming photo's appropriately for data entry to assign it to a Unique ID. Since we're using Mac's this is done in AppleScript. This works great but it's only for one machine. What is now needed is to work on multiple machines. What I want to do is put the photos on our server and have the client machines do the action on the folder from there.
The problem I am currently having is that the script does not authenticate the user and does not run the script even though the info is correct. Am I doing this correctly?
tell application "Finder" of machine "eppc://user:password#server.local"
set renameFiles to the selection
set inventoryFiles to every file in folder (((path to documents folder) as text) & "Inventory Photos")
set currentIndex to the count of inventoryFiles
repeat with i from 1 to the count of renameFiles
set currentFile to (item i of renameFiles)
set new_name to ((10000 + currentIndex + i) as text) & ".jpg"
set name of currentFile to "a" & new_name
end repeat
end tell
Thank you for any help

Resources