Applescript Posix files and paths - templates

I am trying to set a script to duplicate a template file to a newly created folder. The file is a variable created from template_name. Unfortunately I am a posix novice and can't seem to get the script to copy the template file to the new folder.
The returned error is
"Can't set "/Volumes/Media/NewFolder" of application "Finder" to file "Volumes/Media/+A3Temp.ptx" of application "Finder"
set temp_name to client_code & " " & brand_name & " " & job_name & " " & job_number & " " & creation_date
set media_folder to "/Volumes/Media/"
set new_folder to media_folder & temp_name
set template_name to ("+" & suite & "Temp.ptx") as string
set session_name to (client_code & " " & brand_name & " " & job_name & " " & creation_date & ".ptx")
set template to (media_folder & template_name)
tell application "Finder"
duplicate POSIX file template to POSIX file new_folder
end tell
I know the error is with these lines
tell application "Finder"
duplicate POSIX file template to POSIX file new_folder
end tell
But i have no clue as how to remedy it. Can anyone help please!?

I got a similar error when I ran this script:
set f to "/usr/share/dict/connectives"
set d to "/private/tmp/"
tell application "Finder"
duplicate POSIX file f to POSIX file d
end tell
Finder got an error: Can’t set POSIX file "/private/tmp/" to POSIX file "/usr/share/dict/connectives".
Either of these worked though:
set f to POSIX file "/usr/share/dict/connectives"
set d to POSIX file "/private/tmp/"
tell application "Finder"
duplicate f to d
end tell
tell application "Finder"
duplicate POSIX file "/usr/share/dict/connectives" to POSIX file "/private/tmp/"
end tell

Related

Error using VBScript in BGInfo File not found error

I'm an amateur VB scripter.
I am creating a script to output the ID.
The file contains the line "ad.annnet.id = 564654068". It is necessary to output "ID: 564654068"
With New RegExp
.Pattern = "\nID=(\d+)"
Echo .Execute(CreateObject("Scripting.FileSystemObject").OpenTextFile("this.conf").ReadAll)(0).Submatches(0)
End With
There are multiple issues with the script, the actual cause of the "File not found" error is as #craig pointed out in their answer the FileSystemObject can't locate the file "this.conf". This is because the OpenTextFile() method doesn't support relative paths and expects an absolute path to the file whether it is in the same directory as the executing script or not.
You can fix this by calling GetAbsolutePathName() and passing in the filename.
From Official Documentation - GetAbsolutePathName Method
Assuming the current directory is c:\mydocuments\reports, the following table illustrates the behaviour of the GetAbsolutePathName method.
pathspec (JScript)
pathspec (VBScript)
Returned path
"c:"
"c:"
"c:\mydocuments\reports"
"c:.."
"c:.."
"c:\mydocuments"
"c:\"
"c:"
"c:"
"c:.\may97"
"c:.\may97"
"c:\mydocuments\reports*.*\may97"
"region1"
"region1"
"c:\mydocuments\reports\region1"
"c:\..\..\mydocuments"
"c:....\mydocuments"
"c:\mydocuments"
Something like this should work;
'Read the file from the current directory (can be different from the directory executing the script, check the execution).
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim filename: filename = "this.conf"
Dim filepath: filepath = fso.GetAbsolutePathName(filename)
Dim filecontent: filecontent = fso.OpenTextFile(filepath).ReadAll
Update: It appears you can use path modifiers in OpenTextFile() after all (thank you #LesFerch), so this should also work;
'Read the file from the current directory (can be different from the directory executing the script, check the execution).
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim filename: filename = ".\this.conf" '.\ denotes the current directory
Dim filecontent: filecontent = fso.OpenTextFile(filename).ReadAll
Another issue is the current RegExp pattern will not match what you are expecting, would recommend using something like Regular Expressions 101 to test your regular expressions first.
In bginfo, click Custom, New, enter ID for the identifier, Check the VB Script file radio button, and then click Browse to select a VBScript file you have saved with the code below. Then add the "ID" item to the bginfo display area. Note: The "file not found" error is resolved by using the AppData environment variable to reference the AnyDesk data file.
Const ForReading = 1
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
AppData = oWSH.ExpandEnvironmentStrings("%APPDATA%")
DataFile = AppData & "\AnyDesk\system.conf"
Set oFile = oFSO.OpenTextFile(DataFile,ForReading)
Do Until oFile.AtEndOfStream
Line = oFile.ReadLine
If InStr(Line,"ad.anynet.id") Then ID = Split(Line,"=")(1)
Loop
oFile.Close
Echo ID
Please note that the data is returned to bginfo via one or more Echo statements. If testing this script outside of bginfo, change Echo to WScript.Echo.
You will never get the value/display that you're looking for if the output is
File Not Found
The regexp is meaningless until the path to the file is specified. As it stands, the "this.conf" file must be in the same location as the script itself - and from the error, I'm assuming that's not the case.

Why am I getting a "The filename, directory name, or volume label syntax is incorrect." error?

I've been getting an error in this snippet of code that says my file path is incorrect. I know python likes to convert file paths to use double slashes (ie. \) however I am using a raw string variable. Does anyone understand why this is happening?
import os
comList = ['D:\\twidl\\data\\intel\\unlock\\unlock.bin\n', 'D:\\Kit025_02_TGF047K_7002\\BI\\TG-OEM\\Dell\\TGB047K_TGL051b7UB_1024.bin\n', 'D:\\twidl\\gui\\utils\\products.xml\n']
def remanTool():
for string in comList:
string1 = r"C:\Users\mgilmore\Desktop\FirmwareInstaller\WinPython-32bit-2.7.13.0Zero\python-2.7.13\python.exe"
string2 = r"C:\Users\mgilmore\Desktop\FirmwareInstaller\TWIDL\PSHH_Reman.py"
command = os.system(string1 + string2 + " -s " + comList[0] + " -f " + comList[1] + " -m " + comList[2] )
print command
I expect the command to be run, however it keeps saying the file path is wrong.
Also, for reference, comList is basically just an array of directories.
The directories for some reason have been saved with endlines and double slashes. I've already tried using os.path.abspath().
Here is my error message:
The filename, directory name, or volume label syntax is incorrect.
I figured out what was wrong guys, it was just an error in my text editor. All I had to do was restart the software.

How to Zip folder with AppleScript-objc?

I'm generating a few Logs on the system, and then copying it to /tmp/MyFolder, and then
I move the folder to the desktop, I'm trying to compact it before you move on, but I don't know how to do it, I have tried the following:
tell application "Finder"
set theItem to "/tmp/MyFolder" as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
do shell script "zip -r " & zipFile & " " & itemPath
end tell
While this isn't an AppleScript-ObjC script, I'm posting the corrected version of your own script so that it functions to do as you described:
tell application "System Events"
set itemPath to "/tmp/MyFolder"
set theItem to item itemPath
set fileName to quoted form of (get name of theItem)
set theFolder to POSIX path of (container of theItem)
set zipFile to fileName & ".zip"
end tell
do shell script "cd " & quoted form of theFolder & ¬
"; zip -r " & zipFile & space & fileName & ¬
"; mv " & zipFile & space & "~/Desktop/"
Trying to avoid using Finder for file system operations. It sounds counter-intuitive, but it's not well-suited for it. Use System Events, which has—among many other benefits—the ability to handle posix paths.
The script now zips the folder and its containing items into an archive at /tmp/MyFolder.zip, then moves this archive to the desktop.

boost::program_options space in config file

I want to set options to my parser program via simple config file parsed by boost::program_options. I want to set prefixmiddle to (single space).
I already tried that options:
prefixmiddle=
prefixmiddle=" " # Equals to `" "`
prefixmiddle = " " # Same
prefixmiddle =
Is there a way to do it?
P.S. Of course, I can always use "" and remove them, but I wonder if there is another solution.

gdal.driver.create is removing another file in the directory

I've been working with gdal in python for a few years, and the past few days I've found what I suspect may be a bug in the gdal driver's Create command. I'm working with Landsat imagery, and have tried the below code on a few scenes with the same results each time. In certain situations, when I call create, it deletes another file in the directory (always the MTL file).
import gdal
path = '.../LC80110112013243LGN00/' #path to where ever your landsat scene is
outfile = path+path[-22:-1]+'_B5_test.tif'
#outfile = path + 'TestB5.tif'
infile = path+path[-22:-1]+'_B5.tif'
infile_open = gdal.Open(infile)
infile_array = infile_open.GetRasterBand(1).ReadAsArray()
dtype=gdal.GDT_Float32
outfile = gdal.GetDriverByName('GTiff').Create(outfile, infile_array.shape[1], infile_array.shape[0], 1, dtype)
infile_open = None
outfile = None
infile_array = None
If I use the first outfile name, which creates a filename matching the rest of the Landsat band files, and the file "outfile" already exists, it is replaced (expected behavior) and the met file is deleted (unexpected behavior). If I use the second outfile name, which does not match the Landsat band filename format, when I run the code if "outfile" already exists it simply replaces the old file (expected behavior). I have not been able to find any other reference to this happening. Any ideas what's going on?
Similarly!
GDAL Version: GDAL 2.1.3, released 2017/20/01
Paltform: Ubuntu 16.04 LTS
Out of the situation so:
. . .
if os.path.exists(outputFileName):
os.remove(outputFileName)
dst_ds = driver.Create(outputFileName, width, height, bands_value, gdal.GDT_Float32)
. . .