Mars3653
Mars3653

Reputation: 1

custom url generator to open links in browser other than default - beginner, assistance greatly appreciated

unsure if this is classified as a duplicate due to other posts asking about this with different languages or ide's etc.

i am trying to set up an automatic internet shortcut creator/opener, and this being my first programing project(outside of a couple monstrous google sheet formulas(800 ish tokens i think)), and so I'm not 100% sure what I'm doing, but am pretty good at troubleshooting.

baselines accomplishments that work (so far):

I've created 2 new file formats(.txtp and .urlp, using regedit.exe, they both function and have been tested) that automatically launch directly into the python shell. by my fabricated standard, these are no different than txt files, other than the fact that windows 11 home(my operating system) will recognize these differently than a normal .txt file or internet shortcut, as with a normal .txt the operating system will will only have one automatic file launcher for each file type, and so i would have to go through and click "open with..." everytime i open a .txt file with a function other than reading and editing it, and this option isnt always available after ive chosen a default launcher for that file type. keep in mind i can still edit these .txtp and .urlp files with my .txt editor(sublime) by opening the editor itself and going to file/open file then selecting the desired file. same with the .urlp file type, i just want my desktop internet shortcuts to open in a different browser than my normal browser(min and vivaldi, respectfully).

ive also got a decent base of code set up to run, however i dont have an IDE other than sublime, and at ive got to figure out how to get programming related pluggins like anaconda to work, or figure out how to use them if they are.

code i have so far that im having problems with:

url = input('please enter url - ')

file_name = input('please enter file name - ')
file_adress = 'C:\Users\sniez\OneDrive\Desktop\' + file_name + '.urlp'

open(file_name, mode=x, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

# things i need to edit into the file with/after the open() function
   var.url
   #the following is a url format sample(source: https://www.lyberty.com/encyc/articles/tech/dot_url_format_-_an_unofficial_guide.html), since i want the file type to behave the same as a regualr url file, just in a browser other than my default browser, (as far as i can tell that is changed by the working directory line)
   
   [InternetShortcut]
   URL=http://www.someaddress.com/
   WorkingDirectory=C:\WINDOWS\
   ShowCommand=7
   IconIndex=1
   IconFile=C:\WINDOWS\SYSTEM\url.dll
   Modified=20F06BA06D07BD014D
   HotKey=1601


input('press enter to exit')

problems

with the input() function, as far as i can tell, even when used while defining a variable, it should pause the script and let me enter data before it continues and closes the shell window. it does not do this. it flashes in and out of existance very quickly

i also need to edit the file i create using open(... mode=x...), however i cannot find any way to do that that i understand on stack overflow or anywhere, im sure there is something that will tell me how to do that but i havnt found it yet, so i will add it here and if someone has a tutorial link or can explain it quickly id apreciate it very much.

i am also unsure if i need to change the format of the url

this bit isnt important but you can read it if you want

i know i can accomplish the task at hand by simply creating a url shortcut as per normal then naming the extention of it ".urlp" then just having windows recognise that as something that i always open with a specifi browser other than the default, however i have been procrastinating learning how to prgram for 5 ish years now and this a great way to start i think(i would watch one of those youtube series or sometihng however i am very time restrained with general schedule, i cant do much more than tinker with this. typing this question took me 3 days of finding bits and peices of time)

I cant think of any other information i should include, please let me know if you have questions about my question lol

edit#1 i have worked on this a decent bit since i posted, here is the updated code.

    #importing modules
    import string
    import module
    import start

    #taking user imput
    url = input('please enter site url - ')
    filename = string(input("please enter file name - ") + '.py')


    # we now open the file
    file = open.module(('C:/Users/sniez/OneDrive/Desktop/' + filename), mode=w)

    #writing to file
    import webbrowser
    import url

    browser = "C:/Users/sniez/AppData/Local/min/min.exe %s"

    webbrowser.get(f"browser: {browser}").open(f"url: {url}")

    file.close()

    #now starting writen file to show functionality
    start.file


    #variable method friend gave me that i dont know how it works?:
    #number = 1
    #print("number:", string(number))
    #print(f"number: {number}")

I've now verified that the input functions work fine, along with the variables, but the open function is still no working, i am unsure why. again, any help is tremendously appreciated.

Upvotes: -1

Views: 118

Answers (1)

Anony Mous
Anony Mous

Reputation: 7

I believe that what's happening is you're getting an unhandled exception that's causing the script to exit out. That's likely because of the ridiculous number of SyntaxErrors you have in your code: no wonder it doesn't work.

Try this code:

import os


url = input('please enter url - ')

file_name = input('please enter file name - ')
file_adress = os.path.join(r'C:\Users\sniez\OneDrive\Desktop', file_name + '.urlp')

with open(file_name, mode="x", buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) as f:
    f.write("""Insert your text here.
Next line,
Etc.""")

input('press enter to exit')

Here are the issues in your old code:

  1. You can't end a string on a backslash, like you did in file_address. If you want to use single backslashes, you also have to add an r in front of the opening quotation make of a string (e.g., r'string\with\backslash')
  2. When using the open function, specify the mode parameter with a string value, not just x because then the code assumes it's looking for the value of a variable called x (which doesn't exist, causing a NameError. So replace it with 'x'.
  3. This is not as important, but you should use with open(...) as f: ... instead of f = open(...). More of a security and "safety" feature. Perform all your write commands in the with block.
  4. And in future, use a more beginner friendly code editor, like vscode, whose 'integrated' terminal will not exit when an error occurs.

Have fun programming, and next time, don't provide unnecessary details in your question. We do not care that this is your first project.

Upvotes: -1

Related Questions