Automator variable base on file name (Applescript?) - macos

I am trying to use automator to rename files based on the folder I select as the input. I want to take the folder name and if 1 of 4 phrases are found in the folder name, 1 of 4 variables would be used. I don't know applescript but I feel this is the way to go base on other languages I know.
Can anyone convert the following concept?
if file name contains "USA" then
var = "US"
elseif file name contains "CAN_FR" then
var = "CAFR"
elseif file name contains "CAN_EN" then
var = "CAEN"
end
Much appreciation to anyone that can help.

Try:
on run {input, parameters}
tell application "Finder" to set folderName to name of first item of input
if folderName contains "USA" then
set var to "US"
else if folderName contains "CAN_FR" then
set var to "CAFR"
else if folderName contains "CAN_EN" then
set var to "CAEN"
else
set var to "Not Found"
end if
-- insert your code here
return var
end run

Related

Getting path in iPhotoLibrary by photo id using applescript

I try to select the path in the iPhotoLibrary of a photo by its id using the following Applescript:
tell application "iPhoto"
set photoID to 25801
set thePhoto to photo id (photoID + 2 ^ 32)
set photoPath to image path of thePhoto
end tell
That doesn't work because Applescript told me, that it's not possible to convert 4.294993097E+9 to integer. I wrote (photoID + 2 ^ 32) as number and Applescript wasn't able to got the image path.
Please told me what was my mistake and how can I solve the problem.
You must first get the 'Photo' object from the ID by a search in iPhoto, then only get the path of that object:
set myPhoto to first item of (every photo whose id is myId)
set myPath to image path of myPhoto
myPath is the complete file path in Unix format (with'/' and not ':')

Lists in VBscript return object

I'm writing a vbscript function that looks something like this:
Public Function fnGetXLSFileCount()
Dim fso, src, folder, file, fileList
Set fileList = CreateObject("System.Collections.ArrayList")
Set fso = CreateObject("Scripting.FileSystemObject")
src = "\\myserver\myfolder"
Set folder = fso.GetFolder(src)
For Each file In folder.files
If LCase(fso.GetExtensionName(file)) = "xlsx" Then
fileList.Add file.name
End If
Next
Set fnGetXLSFileCount = fileList
End Function
As you can see I'm creating an ArrayList and then adding all the names of excel files that exist in a specified folder.
I then call this function and use the Set operator to specify that I'm expecting an object to be returned.
Set XLSFileList = fnGetXLSFileCount
When I check the count on the object it seems to be correct.
When I try to pull the names out there is nothing there. What am I doing incorrectly here?
For each file in XLSFileList
name = file.Item(0)
Next
The For Each loop already enumerates the items of the collection. And since you assign just the names to the collection you simply use the loop variable to get the name:
For Each file In XLSFileList
name = file
Next
The Item property can be used to directly access a specific item from the collection:
WScript.Echo XLSFileList.Item(0)

How to get a file name from a list of files using link query

I am using linq query to get the filename from a list of file names. but I am getting null value. Is there anything wrong with the code??
var folderName = UserDetailsUtil.GetMemberPhotoPathFolderName(SessionData.UserID);
var fileName = SessionData.UserID;
var fileNames = Directory.GetFiles(Path.Combine(Server.MapPath("~/Content/Upload/MemberProfilePhotos/" + folderName)));
var actualFileName = fileNames.Where(x => x.StartsWith(fileName)).FirstOrDefault();
My image name will be with my userid. Total images in that particular folder comes into filenames. What i have to do is, I need to get the filename from the list of files
I assume that your file has an extension, so you need to find it without it, best by using the Path class:
var actualFileName = fileNames
.Where(fn => Path.GetFileNameWithoutExtension(fn) == fileName)
.FirstOrDefault();
Side-note acc. to "My image name will be with my userid". Then don't use StartsWith but ==, otherwise file 1 and 10 are equal. Changed that already in my code above.

List directory with custom name applescript

I am looking for a way to display this list with a few custom tags.
Could anybody help me out? I only found this script and is very useful to me if I could alter a few parameters. However I have zero applescript experience.
I would like to have img, description and title as extra variable.
So the output would look like this: ( I will fill in title & description in later ofc. )
{
"title": "",
"gallery": [
{
"img": "101.jpg",
"title": "",
"description": ""
},
{
"img": "102.jpg",
"title": "",
"description": ""
}
]
}
etc for all images in that folder
Please find the script that outputs me my content of a chosen folder as an array. which outputs now as {"P1000443.JPG", "P1000444.JPG"}
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
Thanks!
What you're asking is complicated so let me get you started. Run this script, choose a file, and look at the results.
set aFile to choose file
tell application "Finder" to return properties of aFile
You will see all of the properties you can get from a file. Right now in your code you are asking only for the "name" property so you'll have to figure out what properties you want and ask for them in a similar manner as you have asked for the name. Then you'll have to convert all of that information into a nice organized list as you request... that's the difficult part. If you want something that you can't find in the properties then you'll have to figure out how to get that information in another way, for example "description".
EDIT: Based on your comment, I suggest you just use a repeat loop and create a string any way you want. Here's one example. You can use this technique to create the output in any format you want.
set file_list to {"P1000443.JPG", "P1000444.JPG"}
set outputString to ""
repeat with i from 1 to count of file_list
set outputString to outputString & "img: " & item i of file_list & return & "title: " & return & "description: " & return & return
end repeat
return outputString
The output based on the way I formatted it will look like this...
img: P1000443.JPG
title:
description:
img: P1000444.JPG
title:
description:
Good luck.

Checking the folder type

I loop through each folder of my Outlook store to check the folder type by retrieving all default folders and comparing their EntryID.
Public Function GetFolderTypeName(objFolder)
Dim objType, objDefaultFolder
'dctValidFolders is a dictionary of default folders type
For Each objType in dctValidFolders
Set objDefaultFolder = objMAPI.GetDefaultFolder(dctValidFolders.Item(objType))
If objFolder.EntryID = objDefaultfolder.EntryID Then
Set objDefaultfolder = Nothing
GetFolderTypeName = objType
Exit Function
End If
Next
End Function
Now, I have a mailbox that has more than one folder of type contacts.
Is there another way to know the folder type?
There was in fact, check
folder.DefaultItemType
This will return an item type , like olMailItem, and depending on that you can "assume" the folder type.

Resources